Given that bw is a reference to a valid BufferedWriter And the snippet: 15. BufferedWriter b1 = new BufferedWriter(new File("f")); 16. BufferedWriter b2 = new BufferedWriter(new FileWriter("f1")); 17. BufferedWriter b3 = new BufferedWriter(new PrintWriter("f2")); 18. BufferedWriter b4 = new BufferedWriter(new BufferedWriter(bw)); What is the result?

  1. Compilation succeeds

  2. Compilation fails due only to an error on line 15

  3. Compilation fails due only to an error on line 16

  4. Compilation fails due only to an error on line 17

  5. Compilation fails due only to an error on line 18

  6. Compilation fails due to errors on multiple lines


Correct Option: B

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Compilation succeeds - This option is incorrect because the compilation does not succeed. There are errors in the code.

Option B) Compilation fails due only to an error on line 15 - This option is correct. The error on line 15 is that the constructor for the BufferedWriter class expects a Writer object as an argument, not a File object. Therefore, the code fails to compile.

Option C) Compilation fails due only to an error on line 16 - This option is incorrect. Although the constructor for the BufferedWriter class expects a Writer object as an argument, the FileWriter class is a subclass of Writer. So, there is no error on line 16.

Option D) Compilation fails due only to an error on line 17 - This option is incorrect. Although the constructor for the BufferedWriter class expects a Writer object as an argument, the PrintWriter class is a subclass of Writer. So, there is no error on line 17.

Option E) Compilation fails due only to an error on line 18 - This option is incorrect. The code on line 18 is valid. It creates a BufferedWriter object and passes another BufferedWriter object as an argument to its constructor.

Option F) Compilation fails due to errors on multiple lines - This option is incorrect. The code fails to compile only due to an error on line 15.

The correct answer is B. Compilation fails due only to an error on line 15. This option is correct because the constructor for the BufferedWriter class expects a Writer object as an argument, not a File object.

Find more quizzes: