Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Medium
Question 13
Given code of Test.java file:
- package com.udayan.ocp;
- import java.io.*;
- public class Test {
- public static void main(String[] args) {
- try(BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\test.txt")))
- {
- bw.close();
- bw.newLine();
- } catch(IOException e) {
- System.out.println("IOException");
- }
- }
- }
F: is accessible for reading/writing purposes.
Which of the following statement is true about above code?
- A Compilation error
-
B
Class Test compiles and executes fine and no output is displayed on to the console
- C On execution, IOException is printed on to the console
Reveal correct answer
Correct answer: C
Explanation
Once the close() method is called on BufferedWriter, same stream cannot be used for writing.
bw.newLine(); tries to append a newline character to the closed stream and hence IOException is thrown.
catch handler is provided for IOException. Control goes inside and prints 'IOException' on to the console.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
