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

Given:

and the code fragment:

What is the result?

    Correct Answer: B

    The given code uses a try-with-resources statement, which requires the resource to implement the AutoCloseable interface. The MyClass class implements AutoCloseable, and thus it can be used in the try-with-resources block. Inside the try block, the obj1 object is assigned a value of 100 to its 'test' field, and obj is assigned obj1 through the copyObject() method. Since 'obj' and 'obj1' refer to the same object, when the try block ends, both still have their 'test' field set to 100. The program will print the value of obj.test, which is 100. Therefore, the output is 100.

Discussion
Tarik2190Option: B

Answer is B, class MyClass implements AutoCloseable { int test; public void close() { } public MyClass copyObject() {return this;} } public class Test { public static void main (String[] args) throws InterruptedException { MyClass obj = null; try (MyClass obj1 = new MyClass()) { obj1.test = 100; obj = obj1.copyObject(); } System.out.println(obj.test); } }

jduarteOption: B

answer B. Tested

steefaandOption: B

Answer is B. Tested.

duydnOption: B

B is correct, this is Try-with-resource, and need implement AutoCloseable interface.