Java OCA OCP Practice Question 296

Question

How many objects are eligible for garbage collection right before the end of the main method?

1:   public class MyClass { 
2:      public MyClass myField; 
3: /*from   w  w w  .  j  a  v a 2 s  .c  o  m*/
4:      public static void main(String... args) { 
5:         MyClass elena = new MyClass(); 
6:         MyClass diana = new MyClass(); 
7:         elena.myField = diana; 
8:         diana = null; 
9:         MyClass zoe = new MyClass(); 
10:        elena.myField = zoe; 
11:        zoe = null; 
12:     } 
13:   } 
  • A. None
  • B. One
  • C. Two
  • D. Three


B.

Note

On line 9, all three objects have references.

The elena and zoe objects have a direct reference.

The diana object is referenced through the elena object.

On line 10, the reference to the diana object is replaced by a reference to the zoe object.

The diana object is eligible to be garbage collected, and Option B is correct.




PreviousNext

Related