Java OCA OCP Practice Question 3002

Question

Given:

import java.io.File;
import java.io.PrintWriter;

public class Main {
   public static void main(String[] args) {
      try {//w  ww  .jav  a2s. c om
         File f1 = new File("sub1");
         f1.mkdir();
         File f2 = new File(f1, "sub2");
         File f3 = new File(f1, "sub3");
         PrintWriter pw = new PrintWriter(f3);
      } catch (Exception e) {
         System.out.println("ouch");
      }
   }
}

And, if the code compiles, what is the result if "java Main" is invoked TWICE? (Choose all that apply.)

A.Compilation fails.
B.The second invocation produces the output  "ouch".
C.The second invocation creates at least one new file as a peer to the new directory.
D.The first invocation creates a new directory and one new file in that directory.
E.The first invocation creates a new directory and two new files in that directory.
F.The first invocation creates a new directory and at least one new file as a peer to it.


D is correct.

Note

The program creates a new directory called "sub1", and adds a new file called "sub3" to "sub1".

The program creates a File object called "sub2", but it is never used to create a physical file.

The second invocation of the program runs without exception.

The call to mkdir() returns a boolean indicating whether or not a new directory was made.

The second invocation of Main truncates the file "sub3" to zero length, so any data added to the file before the second invocation is lost.




PreviousNext

Related