Java OCA OCP Practice Question 3058

Question

Given:

14.   FileWriter fw1 =  
       new FileWriter(new File("f1.txt"));  
15.   FileWriter fw2 =  
       new FileWriter(new BufferedWriter(new PrintWriter("f2.txt")));  
16.   PrintWriter pw1 =   
       new PrintWriter(new BufferedWriter(new FileWriter("f3.txt")));  
17.   PrintWriter pw2 =  
       new PrintWriter(new FileWriter(new File("f4.txt"))); 

And given the proper imports and error handling, what is the result?

  • A. Compilation succeeds.
  • B. Compilation fails due to multiple errors.
  • C. Compilation fails due only to an error on line 14.
  • D. Compilation fails due only to an error on line 15.
  • E. Compilation fails due only to an error on line 16.
  • F. Compilation fails due only to an error on line 17.


D is correct.

Note

The code is using so-called "chained-methods" (or constructors) syntax.

The objects fw1, pw1, and pw2 are created correctly.

The object fw2 cannot be created because a FileWriter cannot be constructed using a BufferedWriter object.




PreviousNext

Related