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.

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