Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 58
Question ID: UKOCP22028
Given code of Test.java file:
- package com.udayankhattry.ocp;
- class Resource implements AutoCloseable {
- public void close() {
- System.out.println("CLOSE");
- }
- }
- public class Test {
- public static void main(String[] args) {
- try(Resource r = null) {
- r = new Resource();
- System.out.println("HELLO");
- }
- }
- }
What will be the result of compiling and executing Test class?
- A HELLO
-
B
HELLO
CLOSE
- C Compilation error
- D NullPointerException is thrown at runtime
Reveal correct answer
Correct answer: C
Explanation
UKOCP22028:
Variable r is implicitly final and hence can't be re-initialized.
'r = new Resource();' causes compilation error.
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.
