jQuery in Action, 2nd edition*
The moose likes Mock Exam Errata and the fly likes Barry Boone (BB) Q38 FileOutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Barry Boone (BB) Q38 FileOutputStream " Watch "Barry Boone (BB) Q38 FileOutputStream " New topic
Author

Barry Boone (BB) Q38 FileOutputStream

Daniel Wu
Ranch Hand

Joined: Jun 13, 2001
Posts: 58
Greetings everyone!
Regardng Question 38(it is attached later), BB choose a. BB says, the first line creates a File object that represents the file. By creating a FileOutputStream, you create the file if it does not yet exist, and open that file for reading and writing.
I think d is right because FileOutputStream is not for reading, and it is for writing only; reading is the job of FileInputStream.
I seek your advice. Thank you very much.
Question 38:
What does the following code do? File f = new File("hello.test");
FileOutputStream out = new FileOutputStream(f);

Select the one right answer.

a. Create a new file named "hello.test" if it does not yet exist. It also opens the file so you can write to it and read from it.
b. Create a new file named "hello.test" if it does not yet exist.
The file is not opened.
c. Open a file named "hello.test" so that you can write to it and read from it, but does not create the file if it does not yet exist.
d. Open a file named "hello.test" so that you can write to it but cannot read from it.
e. Create an object that you can now use to create and open the file named "hello.test," and write to and read from the file.
Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
Hi,
In my opinion option e is right.
Any doubts? Please correct me if I am wrong
Jyotsna
Daniel Wu
Ranch Hand

Joined: Jun 13, 2001
Posts: 58
Could you tell me why e is right?
Jyotsna Umesh
Ranch Hand

Joined: May 09, 2001
Posts: 94
I chose option e becoz by constructing instance of file doesn't mean that new file is created on the system but it actually means that an object has been created, now you can actually create and open the file named "hello.test," and write to and read from the file.
I hope I am clear
Jyotsna
Daniel Wu
Ranch Hand

Joined: Jun 13, 2001
Posts: 58
Jyotsna,
FileOutputStream class has no read methods, so it seems to me that the codes in question could not "read from a file". Right?
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Daniel, Jyotnsa,
The <code>File</code> class allows you treat a file as either an input or output stream ie it opens a file in 'read' and 'write' modes.
In the example question, the file is only given to an output stream;however, it could just as easily have been assigned to an input stream.
So, Jyotnsa, you are right. The given code could not write to the file unless it was passed to a FileInputStream however the <code>File</code> class itself opens the file for both reading and writing.
Hope that helsp.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
yaeger rosenberg
Greenhorn

Joined: Aug 04, 2001
Posts: 6
Jane:
Are you sure you are right on this one? I am confused (and maybe just missed the obvious, but)
isn't it true that the File declaration has NO EFFECT on the underlying file system; it does not open any file, doesn't check if the file exists, etc. So it certainly can't open the file for reading and writing. It may be used later in another constructor for a stream/reader class to open either for input or output or in an ra file for both. But
Since the next line uses the File object to instantiate an output stream, which the API doc says opens for writing, how can the answer possibly be for reading and writing?
While I am at it, I am going through the past year's archive on mock exam errata questions. I find them an excellent way to review for the exam. THANK YOU for all the work you put in answering.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
d. Open a file named "hello.test" so that you can write to it but cannot read from it.
You can read using the File object, just not with the FileOutputStream.
e. Create an object that you can now use to create and open the file named "hello.test," and write to and read from the file.
The File object can be used to create ,open ,write to, and read from so in that sense this is correct.
I think this is one of those poorly worded mock questions that you will never see in a real exam.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
D is correct.
The qwuestion is:
*What does the following code do?*
File f = new File("hello.test");
FileOutputStream out = new FileOutputStream(f);
Select the one right answer.
So a and b are wrong, since nothing is created file-wise.
a. Create a new file named "hello.test" if it does not yet exist. It also opens the file so you can write to it and read from it.
b. Create a new file named "hello.test" if it does not yet exist.
The file is not opened.
c and e are wrong since here is all about write
c. Open a file named "hello.test" so that you can write to it and read from it, but does not create the file if it does not yet exist.
e. Create an object that you can now use to create and open the file named "hello.test," and write to and read from the file.
S only d is right.
d. Open a file named "hello.test" so that you can write to it but cannot read from it.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
a is definitely wrong because the file is opened for writing to only. If you want to read from it you need to create a FileInputStream and that will open it for reading.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
just an example for Tom's explanation.

See, we can read and write to the file which is created by FileOutputStream. But in the question they don't have FileInputStream, so the (question)code can write but not read.
Vanitha.
[This message has been edited by Vanitha Sugumaran (edited August 07, 2001).]
having problem in font size
[This message has been edited by Vanitha Sugumaran (edited August 07, 2001).]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
But it will report true even without creating any Streams against the File object. You can't actually read from the file until you create an InputStream. the canRead() just lets you know whether you will be able to read from it if you create an InputStream.
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Tom,
I wrote "can read" in the previous post in the meaning of able to read.
Thanks for your explanation.
Vanitha.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I think the conclusion is that the question is poorly worded. It is unlikely that you will see a question like that on the certification test.
------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
 
wood burning stoves
 
subject: Barry Boone (BB) Q38 FileOutputStream
 
Similar Threads
Barry Mock Exam
Boone's exam: FileOutputStream
Boones' Exam Question Doubt
Some confusing ones part2
Boone #38; File I/O