• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock exam Question.Please anybody explain the output

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1Q)What output is displayed by the following program?
import java.io.*;
public class TestIOApp {
public static void main(Strin args[]) throws IOException {
RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
file.writeBoolean(true);
file.writeInt(123456);
file.writeInt(7890);
file.writeLong(1000000);
file.writeInt(777);
file.writeFloat(.0001f);
file.seek(5);
System.out.println(file.readInt());
file.close();
}
}
A)123456
B)7890
C)1000000
D)777
E).0001
The answer is B)How? Please anybody explain it.

2Q)What is the value displayed by the following program?
class Question {
static boolean sideEffect(boolean b) {
System.out.print("side effect");
return b;
}
public static void main(String[] args) {
boolean b1 = true;
boolean b2 = false;
if(b2 & sideEffect(b1)) System.out.println(1);
else if(b1 | sideEffect(b2)) System.out.println(2);
}
}
A)1
B)2
C)side effect 1
D)side effect 2
E)side effect side effect 1
F)side effect side effect 2
The answer is F). How? Please anybody explain it.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can try to explain the first question.
//byte locations
//written to
file.writeBoolean(true); //(byte 1)
file.writeInt(123456); //(byte 2 - 5)
file.writeInt(7890); //(byte 6 - 9)
file.writeLong(1000000);
file.writeInt(777);
file.writeFloat(.0001f);
file.seek(5); // seeks to byte location 5
System.out.println(file.readInt()); // starts reading for an int at byte location 6
so it reads in 7890

 
Meena
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I could no get your output.
How does the boolean get byte 1,
Int gets byte 2 - 5, Int gets 6 - 9
Will you try to explain it.

 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The comments by each line of Rich's example are the size of the appropriate Java variable in bytes. These sizes are defined in the Java language spec. A boolean is stored as one byte, an integer is four bytes and so on.
When such items are written to a file (or any other stream), this specifies the number of bytes of output that they produce on the stream.
The only correction I would make with Rich's example is to number the bytes starting at zero rather than one. This matches more closely with the 'seek' operation:

[This message has been edited by Frank Carver (edited October 14, 1999).]
 
Meena
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,Frank.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rich Wright:

b2 & sideEffect(b1) is evaluated to false byt & operator evaluates both operands so you get the first side effect. | evaluates to true and there you get side effect 2


 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since & and I or Logical Operators both the Operands of these operators are evaluated and hence on first & evaluation sideeffect is printed and in second expression evaluation sideeffect2 is printed.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As far as the second question is concerned,in first loop,b2 is false and sideEffect(b1) gets evaluated and prints "side effect" and returns true.However,as b2 is false alreday, loop does not get evaluated hence no "1" in out put please!.Now in continuation, it goes to second loop and evaluates sideEffect(b2) which prints out "side effect" and as the b1 =true in this loop, it gets executed and prints "2" in the output.However short ciurcuited parameters will behave differently.Hope this would clear your doubt.
Jaypii
reply
    Bookmark Topic Watch Topic
  • New Topic