GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Boones' Exam Question Doubt 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 "Boones Watch "Boones New topic
Author

Boones' Exam Question Doubt

swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
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.
Answer given - c
Is it correct??
Thanx


Swati Kale
SCJP
SCWCD
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
I don't think so.
A new file will be created if it does not exist .
James Hobson
Ranch Hand

Joined: Aug 28, 2001
Posts: 140
The file is not created until you write to it, simply creating a File Object coes not actually do anything (not to the filesystem anyway!)
Originally posted by swati bannore:
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.
Answer given - c
Is it correct??
Thanx

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I agree with Angela, I don't think C is the correct answer.
Creating a File object has no effect on the file system. But creating a FileOutputStream object with a File argument has the following effect:
1. if the file represented by f exists, the contents of the file will be overwritten
2. if the file does not exist, it is created
The thing here is that you can ONLY write to the file and not read from it since you are using a FileOutputStream, so A, C and E are false. Then between B and D, I would opt for D since B says that the file is not opened and that's not true.
Hopes that helps,
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
I think the correct ans. is b not c.It creates the file if not present but does it open it?i think not.
Usha Damarla
Greenhorn

Joined: Aug 29, 2001
Posts: 13
The following lines are taken from sun's web site http://java.sun.com/docs/books/jls/first_edition/html/javaio.doc16.html

""""22.16.2
public FileOutputStream(File file)
throws SecurityException, FileNotFoundException
This constructor initializes a newly created FileOutputStream by opening
a connection to an actual file, the file named by file in the file system. A new FileDescriptor object is created to represent this file connection.
First, if there is a security manager, its checkWrite method (�20.17.21) is called with the path represented by the file argument as its argument.
If the actual file cannot be opened, a FileNotFoundException is thrown.""""
My answer is D.
Justification:
The first line creates a new File called "hello.txt" if it does not exist. Nothing will happen if the file already exists.
checkWrite method in Securitymanager class throws SecurityException if writing, modifying, creating (for output), or renaming the specified file or directory is not permitted.
we cannot write data if html.txt is not opened.
FileOutputStream constructor throws a fileNotFoundException if the actual file cannot be opened.
The list of possible answers for this question does not mention anything about exception, it means the file is ready to be written.
 
GeeCON Prague 2014
 
subject: Boones' Exam Question Doubt