• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock exam Question.Please anybody explain the output

 
Meena
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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.
 
Rich Wright
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
  • 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.

 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks,Frank.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic