File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JavaCaps Test 2, Question # 34 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 "JavaCaps Test 2, Question # 34" Watch "JavaCaps Test 2, Question # 34" New topic

JavaCaps Test 2, Question # 34

Tina Ang
Ranch Hand

Joined: Mar 12, 2002
Posts: 58
Which line of the program could be throwing an exception, if the program is as listed below. Assume that "MyFile2.txt" is a read only file.

Note: MyFile2.txt is read only file..

A) This program never throws IOException
B) The line f1.close() throws IOException
C) While writing to the stream f1.write(buffer[i]) throws an IOException
D) While constructing the FileWriter f2 = new FileWriter("MyFile2.txt");

Answer is D. Could someone please explain why this is the answer.
Thank you!

Edited code to fit page better
[ April 23, 2002: Message edited by: Jessica Sant ]
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5382
FileWriter is subclass of FileOutputStream and when FileOutputstraem construtor is created then

1) If the file does not exist, it is created.
2) If is exists, its contents are reset, unless the appro. construtor is used to tell that o/p should be appended to the file.
3) A SecurityException is thrown if file does not have write access or can not be created

source - Khalid

"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Oscar Garcia

Joined: Mar 05, 2002
Posts: 23
hi tina!
Note that it was assumed that the given file is
a "read-only" file. So, it will fail when trying to write to that file...remember, it is not writable...
hope this helps...ayos ba? =)

Oscar S. Garcia<br />SCJP<p>--------------------------<br />Pilipino ako...ayos ba?<br />--------------------------
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
from Ravish
FileWriter is subclass of FileOutputStream and when FileOutputstraem construtor is created then

FileWriter is a subclass of OutputStreamWriter, not FileOutputStream.

best regards,<br />Steven<br />SCJP, SCEA
Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
Since the MyFile2.txt is read only,constructing a file writer to write on that file will cause a security exception to be thrown.Hence the answer d is correct.
I agree. Here's the link:
subject: JavaCaps Test 2, Question # 34
It's not a secret anymore!