• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RandomAccessFile

 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you boolean & integer occupies how many bytes...
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic