This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about RandomAccessFile (1.2 exam) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about RandomAccessFile (1.2 exam)" Watch "Question about RandomAccessFile (1.2 exam)" New topic
Author

Question about RandomAccessFile (1.2 exam)

Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109
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.


I not only use all the brains that I have, but all that I can borrow. [Laurence J. Peter]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Karin Paola Illuminate
Ranch Hand

Joined: Oct 18, 2002
Posts: 109
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

Joined: Aug 03, 2002
Posts: 7729
You got what it takes!!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question about RandomAccessFile (1.2 exam)