• 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

question from javacaps(please explain)

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anybody explain me the answer for this,it is better if the explaination is in detail.
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
regards
vasanthi
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HERE IN THIS CODE U R SEEKING THE POSITION TO BE THE 5 TH POSITION AND THEN READING AN integer seek will go to the 5th byte position and that is the 123456 u got to know the size of each primitive type to know the position
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vasu,
What Cherry did not tell u is that the boolean is on byte and the int is four bytes so that's how u end up with a 7890 on position five.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The real answer is that this won't compile. With RandomAccessFile, you need to catch FileNotFoundException and the write methods throw IOException. So you would need to catch these in this method.
However, with that aside, the right answer is B. 7890. Cherry has the right idea, but the wrong answer. A boolean is one bit, but the writeBoolean will write one byte. And int have 4 bytes. So when you seek to the 5th byte, you pass the boolean and the first int, then you read the next int, or next 4 bytes to give you 7890.
Bill
 
Val Gueorguiev
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good work Bill. That says it all.
 
Ranch Hand
Posts: 144
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Vasu,
With your code I had put a test.txt file in c:\
however the program throws an exception."File not found".
Perhaps ,I am doing some silly mistake.....
But what ...???
_______________
import java.io.*;
public class TestIPApp {
public static void main(String args[]) {
try{
RandomAccessFile file = new RandomAccessFile("c:\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(Exception e)
{
System.out.println("I am flawed"+e);
}

}
}
________________
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try "c:\\test.txt" instead. The \t is a tab character.
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bill bozeman:
The real answer is that this won't compile. With RandomAccessFile, you need to catch FileNotFoundException and the write methods throw IOException. So you would need to catch these in this method.
However, with that aside, the right answer is B. 7890. Cherry has the right idea, but the wrong answer. A boolean is one bit, but the writeBoolean will write one byte. And int have 4 bytes. So when you seek to the 5th byte, you pass the boolean and the first int, then you read the next int, or next 4 bytes to give you 7890.
Bill


Thankz Bill
You are a compiler.
and the reason why writeBoolean write one byte is because we cant write a one bit value of 1 or 0 in a file and get it to be true and false am i right Bill
Once again thankz Bill
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic