Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 80

Given the code fragment:

public static void main (String[] args) throws IOException {

BufferedReader brCopy = null;

try (BufferedReader br = new BufferedReader (new FileReader("employee.txt"))) { // line n1 br.lines().forEach(c -> System.out.println(c)); brCopy = br; //line n2

}

brCopy.ready(); //line n3;

}

Assume that the ready method of the BufferedReader, when called on a closed BufferedReader, throws an exception, and employee.txt is accessible and contains valid text.

What is the result?

    Correct Answer: D

    The code opens the employee.txt file, reads its content, and prints each line. Due to the try-with-resources statement, the BufferedReader object 'br' is automatically closed at the end of the try block. Consequently, when 'brCopy.ready()' is called on the subsequently assigned 'brCopy' (which references the already closed 'br'), it throws an exception. Therefore, the code prints the content of the employee.txt file and throws an exception at line n3.

Discussion
M_JawadOption: D

The code prints the content of the employee.txt file and throws an exception at line n3 . answer is D

asdfjhfgjuaDCVOption: D

D is the correct answer

steefaandOption: D

Answer is D. Prints contents of file and then closes BufferedReader (try with resources) which means that exception is thrown since BufferedReader is used after it is closed.

WilsonKKerllOption: D

Answer is D.

thetechOption: C

There is no compilation error. That means the answer is C