wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes RandomAccessFile 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 "RandomAccessFile" Watch "RandomAccessFile" New topic
Author

RandomAccessFile

Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
Hi,
This is from one of the mock tests.
The answer is b. The explanation given to this is....
The number 7890 is stored at file location 5 since the previously written boolean & int values occupy 5 bytes.
Can anyone explain me the output ?

Import java.io.* ;

class Test{
public static void main(String args[]) throws IOException {
RandomAccessFile f = new RandomAccessFile("test.txt","rw") ;
f.writeBoolean(true) ;
f.writeInt(123456) ;
f.writeInt(7890);
f.writeInt(777);
f.writeFloat(.0001f);
f.seek(5);
System.out.println(f.readInt()) ;
f.close();
}
}
a. 123456
b. 7890
c. 1000000
d. 777
e. .0001
Thx in advance.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Do you boolean & integer occupies how many bytes...
Savithri Devaraj
Ranch Hand

Joined: Jun 26, 2000
Posts: 103
Originally posted by java:
Hi,
This is from one of the mock tests.
The answer is b. The explanation given to this is....
The number 7890 is stored at file location 5 since the previously written boolean & int values occupy 5 bytes.
Can anyone explain me the output ?

Import java.io.* ;

class Test{
public static void main(String args[]) throws IOException {
RandomAccessFile f = new RandomAccessFile("test.txt","rw") ;
f.writeBoolean(true) ;
f.writeInt(123456) ;
f.writeInt(7890);
f.writeInt(777);
f.writeFloat(.0001f);
f.seek(5);
System.out.println(f.readInt()) ;
f.close();
}
}
a. 123456
b. 7890
c. 1000000
d. 777
e. .0001
Thx in advance.

What don't you understand here?
A boolean is a true/false - hence written as 1 byte.
int in java is 4 bytes.
This is a total of 5 bytes - 0 thru 4
the int at position 5 is 7890. That is answer b.
Savithri
 
Consider Paul's rocket mass heater.
 
subject: RandomAccessFile
 
Similar Threads
about file class
seek method in RandomAccessFile
question from javacaps(please explain)
i/o question any help
io--seek()