• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boones' Exam Question Doubt

 
swati bannore
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so.
A new file will be created if it does not exist .
 
James Hobson
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vedhas Pitkar
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic