Java OCA OCP Practice Question 2770

Question

Given the proper import statements and:

23.  try {  
24.    File file = new File("myFile.txt");  
25.    PrintWriter pw = new PrintWriter(file);   
26.    pw.println("line 1");  
27.    pw.close();  
28.    PrintWriter pw2 = new PrintWriter("myFile.txt");  
29.    pw2.println("line 2");  
30.    pw2.close();  
31.  } catch (IOException e) { } 

What is the result? (Choose all that apply.)

  • A. No file is created.
  • B. A file named "myFile.txt" is created.
  • C. Compilation fails due to an error on line 24.
  • D. Compilation fails due to an error on line 28.
  • E. "myFile.txt" contains only one line of data, "line 1"
  • F. "myFile.txt" contains only one line of data, "line 2"
  • G. "myFile.txt" contains two lines of data, "line 1" then "line 2"


B and F are correct.

Note

Both PrintWriter constructors are valid.

When the second constructor is called, it truncates the existing file to zero size, then "line 2" is added to the file by the second println().




PreviousNext

Related