aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock exam Question.Please anybody explain the output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock exam Question.Please anybody explain the output" Watch "Mock exam Question.Please anybody explain the output" New topic
Author

Mock exam Question.Please anybody explain the output

Meena
Greenhorn

Joined: Sep 29, 1999
Posts: 12
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

Joined: Oct 08, 1999
Posts: 17
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

Joined: Sep 29, 1999
Posts: 12
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

Joined: Jan 07, 1999
Posts: 6920
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).]


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Meena
Greenhorn

Joined: Sep 29, 1999
Posts: 12
Thanks,Frank.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Nov 22, 2008
Posts: 18944

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mock exam Question.Please anybody explain the output