Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RandomAccessFile

 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you boolean & integer occupies how many bytes...
 
Savithri Devaraj
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic