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

Question about RandomAccessFile (1.2 exam)

 
Karin Paola Illuminate
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.
 
Barry Gaunt
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!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic