wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes #60 FROM JAVA CAP 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 "#60 FROM JAVA CAP " Watch "#60 FROM JAVA CAP " New topic
Author

#60 FROM JAVA CAP

Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
60. What is the output displayed by the following code?
import java.io.*;
public class TestIPApp {
public static void main(String args[]) {
RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
file.writeBoolean(true);
file.writeInt(123456);
file.writeInt(7890);
file.writeLong(1000000);
file.writeInt(777);
file.writeFloat(.0001f);
file.seek(5);
System.out.println(file.readInt());
file.close();
}
}
Select correct answer:
A) 123456
B) 7890
C) 1000000
D) .0001
could anyone explain the o/p?
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
the answer is 7890, with a try catch block, for IOException, of course. - that is simply reading an int that was written on to the file earlier i guess !
Latha Kalaga
Ranch Hand

Joined: Nov 13, 2000
Posts: 96
Here is why the output is 7890:
file.seek(5); /* this statement moves the pointer to the beginning of the 5th byte i.e current + d bytes where d in this case is 5, current position is at 0 */
- note that boolean does not have a bit width. So the write Boolean doesn't count.
- the next writeInt is 4 bytes long, Seek skips these 4 bytes and postions the pointer at the 5th byte which is the start of the next integer which is your 7890.
Hope this helps.
Latha
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
thanks latha... but how do we konw the length in bytes...
imean, how did you say 123456 is 4 bytes long?
Charlie Swanson
Ranch Hand

Joined: Jan 29, 2001
Posts: 213
Can anyone explain why the boolean value did not occupy any space.
I thought boolean values used 1 bit for the true of false.
I understand that an integer is always 32 bits in Java. and that would make 4 bytes.
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
i guess that the boolean is treated as one byte.
the FilePointer moves the 5 bytes with 1 byte from the boolean and 4 bytes from the integer.
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
What is the smallest unit of information that can be read or written to a file? I assume that the smallest unit is a byte. So I think that writing a boolean to a file will actually mean writting a byte.


----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
the boolean definitely takes up 1 byte, for if u comment that line out, the o/p is some : 2019840, and then if u change the seek ptr to 4 u get 7890 back. it's upon us to imagine the ptr position beginning at 0 or that the next byte is written from the last position onwards !
Judy YU
Ranch Hand

Joined: Nov 19, 2000
Posts: 30
I definitely think a boolean value takes a bytes. I confirmed it with following code:
import java.io.*;
public class TestIPApp {
public static void main(String args[]) throws IOException {
RandomAccessFile file = new RandomAccessFile("test3.txt", "rw");
System.out.println(file.getFilePointer() ); //0
file.writeBoolean(true);
System.out.println(file.getFilePointer() ); //1
System.out.println(file.length() );//1
file.writeInt(123456);
System.out.println(file.getFilePointer() );//5
System.out.println(file.length() );//5
file.writeInt(7890);
System.out.println(file.getFilePointer() );//9
System.out.println(file.length() );//9
file.writeLong(1000000);
System.out.println(file.getFilePointer() );
System.out.println(file.length() );
file.writeInt(777);
file.writeFloat(.0001f);
file.seek(5);
System.out.println(file.readInt());//7890
file.close();
}
}
I think the key is how we understand seek(int offset) method. seek(5) actually the next written/read byte is at position 6, like in array, index 5, because the first byte is at position 0.
Judy
Latha Kalaga
Ranch Hand

Joined: Nov 13, 2000
Posts: 96
Sorry Anshuman, I had disappeared. My ISP was down. Anyway, I guess by now you have the answer already. If else what I meant was that since that line is doing a writeInt and int occupies 4 bytes, I said that is how much 123456 will occupy.
Latha
Originally posted by Anshuman Acharya:
thanks latha... but how do we konw the length in bytes...
imean, how did you say 123456 is 4 bytes long?

natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
Somebody wrote this:
the boolean definitely takes up 1 byte, for if u comment that line out, the o/p is some : 2019840, and then if u change the seek ptr to 4 u get 7890 back.
So what is the junk value getting printed out?. Is it significant?. I tried to find out how it is getting printed. But not successful. I think it is some memory address. Shall we get such questions in the exam?
thanx.

Sun Certified Programmer for Java 2 Platform
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
the value that is getting printed i.e. 2019840, is not a junk value. it is the decimal value of an integer formed from 3 least significant bytes of 7890 and 1 most significant byte of the next long value, 1000000.
when u set the seek value to 5, and the boolean statement is commented out, this is the integer formed by the next 4 bytes.
[This message has been edited by Sweekriti Engineer (edited February 01, 2001).]
shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
hi!
When i try to compile to code it gives a type error on line 4.
the code is as follows
import java.io.*;
public class Test {
try {
public static void main(String args[]) {
RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
file.writeBoolean(true);
file.writeInt(123456);
file.writeInt(7890);
file.writeLong(1000000);
file.writeInt(777);
file.writeFloat(.0001f);
file.seek(5);
System.out.println(file.readInt());
file.close();
}
}
catch(FileNotFoundException e) {
System.out.println(e);
}
}
what is my mistake.please explain.Thanks in advance.
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
Please put the try-catch block inside the main method. Secondly u must catch IOException.
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
Hi Swwekriti engineer,
Thanks.
I too thought the same line. But i didn't build upon that after getting an output of 256 with the foll. code. Sure the junk is not a big value but how do i get that number?. Can u please explain me with the simple numbers i have tried below:
import java.io.*;
public class Randfile {
public static void main(String args[]) throws IOException{
RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
//file.writeBoolean(true);
file.writeInt(0);
file.writeInt(1);
file.writeInt(0);
file.writeInt(77);
file.writeFloat(.0001f);
file.seek(5);
System.out.println(file.readInt());
file.close();
}
}
o/p:256
thanks.
S Dave
Ranch Hand

Joined: Jan 28, 2001
Posts: 103
file.writeInt(0);
file.writeInt(1);
file.writeInt(0);
file.writeInt(77);
file.writeFloat(.0001f);
file.seek(5);
your nos. are written onto the file as:
00000000 00000000 00000000 00000000 --0
00000000 00000000 00000000 00000001 --1
00000000 00000000 00000000 00000000 --0
00000000 00000000 00000000 01001101 --77
if u count from left to right 4 bytes from 5th pointer position are 00000000 00000000 00000001 00000000 which is 256 in decimal.
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
thanks a lot. Thats a beauty.
Kalpesh Soni
Ranch Hand

Joined: Jan 02, 2001
Posts: 312
Hi friends,
i have done some changes to the code so that you can actually see the bit pattern emerging due to writing
first i wrote the file
and then read it byte by byte and printed the byte pattern
THE BOOLEAN HAS TO TAKE SOME SIZE SO THAT IT CAN STORE THE VALUE
AND IT IS ONE BYTE(THE SAMLLEST THING A FILE CAN WRITE!!)
- I will also like to tell you that
seek will act like a cursor so when you say seek(5)
the next thing read will be 6th byte and not the 5th
coz counting starts from 0
i hope it will help

'

'
------------------
KS

[This message has been edited by kalpesh soni (edited February 03, 2001).]
[This message has been edited by kalpesh soni (edited February 03, 2001).]


Test 094, IBM WID 6.0 cert
SCJP 1.2
SCBCD 1.3 Beta
SCWCD 1.4 Beta
SCMAD Beta
SCDJWS Beta

KS
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: #60 FROM JAVA CAP
 
Similar Threads
about file class
seek method in RandomAccessFile
question from javacaps(please explain)
RAF question from javacaps mock, help requested
io--seek()