my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes RAF question from javacaps mock, help requested Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "RAF question from javacaps mock, help requested" Watch "RAF question from javacaps mock, help requested" New topic
Author

RAF question from javacaps mock, help requested

sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
60. What is the output displayed by the following code?
import java.io.*;
public class TestIPApp {
public static void main(String args[]) {
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();
}
}
Select correct answer:
A) 123456
B) 7890
C) 1000000
D) .0001

THE CORRECT ANSWER IS B, HOWEVER I THOUGHT THAT SEEK(5) WOULD START INDEXING FROM 0 AND WOULD STOP AT THE FLOAT VALUE OF .0001.
PLEASE CLEAR MY CONCEPTIONS OF THIS SEEK METHOD, THANKS !
ravi chan
Greenhorn

Joined: Nov 02, 2000
Posts: 11
Sarim,
boolean takes 1 byte, int takes 4 and long takes 8 bytes.
so file.seek(5) is at the 5th byte in the file which is nothing but 7890.
the file looks like this
true 123456 7890 1000000 and so on
1byte]4byte]4byte]------
1 + 4 ] 5th byte starts right at 7 and the whole int is printed - 7890
sarim raza
Ranch Hand

Joined: Nov 02, 2000
Posts: 232
THANKS A LOT RAVI, THAT WAS GREAT !!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: RAF question from javacaps mock, help requested
 
Similar Threads
seek method in RandomAccessFile
question from javacaps(please explain)
i/o question any help
Reg. RandomAccessFile
io--seek()