Question 38: What does the following code do? File f = new File("hello.test");
FileOutputStream out = new FileOutputStream(f); Select the one right answer.
1. Create a new file named "hello.test" if it does not yet exist. It also opens the file so you can write to it. 2. Create a new file named "hello.test" if it does not yet exist. The file is not opened. 3. 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. 4. Open a file named "hello.test" so that you can write to it but cannot read from it. 5. 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.
Given answer : Answer 1
I find "4" also correct.
Answer explanation is given as : Question 38: a. 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. (See chapter 11.)
Indeed, using the FileOutputStream you can only write to the file. However, a file opened by a FileOutputStream, can still be used simultaneously by a FileInputStream to write data to the file. So it is possible to read and write to the file.
The reason why answer 4 is probably not correct is that it doesn't state it will create the file if it doesn't exist.
I also recall a statement in the bold notes box saying, anytime you encounter "Stream" word , it means you only have to worry about synchronization objectives and not IO objectives. This question is more pertaining to IO objective, but its always good to know more.