Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 156

Given:

What is the result?

    Correct Answer: A

    The code will fail to compile because in a try-with-resources statement, the resource `reader` is effectively final. Once a BufferedReader object is initialized within the try-with-resources, it cannot be reassigned. In the provided code, after the first file is read, the code attempts to reassign `reader` to a new BufferedReader object for the second file, which is not allowed. As a result, a compilation error will occur.

Discussion
ASPushkinOption: A

In both cases reader1 is effectively final : BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt")); try (reader1) { System.out.println(reader1.readLine()); //reader1 = Error effectively final } catch (IOException e) { System.out.println(reader1); } } or try (BufferedReader reader1 = new BufferedReader(new FileReader("journaldev.txt"));) { System.out.println(reader1.readLine()); //reader1 = Error effectively final } catch (IOException e) { }

d7bb0b2Option: A

Variables in try with resource are implicit final, so if you try to asing a new value in try block no compile time error.

OmnisumemOption: A

Tested: A. auto-closeable resource reader may not be assigned reader = new BufferedReader (new FileReader(file2)); ^ 1 error error: compilation failed

StavokOption: A

A is correct Exception in thread "main" java.lang.Error: Unresolved compilation problem: The resource reader of a try-with-resources statement cannot be assigned