This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Question about RandomAccessFile (1.2 exam)

 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've got a question about a RandomAccessFile (1.2 stuff).
http://www.javacaps.com/scjp_io.html
17. What is the output displayed by the following code?

[a] 123456
[b] 7890
[c] 1000000
[d] .0001
The answer is [b] 7890.
I' ve got no clue. Can someone explain?
Thanks.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thing you must know is the amount of storage used by each of those primitive types. For example, a boolean needs 1 byte.
Then you can use some squared paper and mark off, in order, boxes for each of those primitives that are written to the file. The length of each box should represent the number of bytes needed for the datatype it contains. Write in each box the data it contains. For example, the first box contains true, the second box contains the value 123456.
Then you must seek byte 5 (starting from the beginning, counting from 0).
You must then read the number contained in the next x bytes where x is the length of a primitive int.
If you don't get 7980, I haven't explained it well enough
[ March 25, 2003: Message edited by: Barry Gaunt ]
 
Karin Paola Illuminate
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry.
I've got it.
seek(0) will position before byte of boolean (true)
seek(1) will position before byte of integer (123456)
an integer is 4 bytes
seek(5) will position before byte of second integer (7890)
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You got what it takes!!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!