OCA Java SE 8 Building Blocks - OCA Mock Question Building Block 23








Question

Suppose we have a class named Main. Which of the following statements are true?

     
     1:public class Main { 
     2:   public static void main(String[] args) { 
     3:     Main one = new Main(); 
     4:     Main two = new Main(); 
     5:     Main three = one; 
     6:     one = null; 
     7:     Main four = one; 
     8:     three = null; 
     9:     two = null; 
    10:     two = new Main(); 
    11:     System.gc(); 
    12: } 
    13:} 
  1. The Main object from line 3 is first eligible for garbage collection immediately following line 6.
  2. The Main object from line 3 is first eligible for garbage collection immediately following line 8.
  3. The Main object from line 3 is first eligible for garbage collection immediately following line 12.
  4. The Main object from line 4 is first eligible for garbage collection immediately following line 9.
  5. The Main object from line 4 is first eligible for garbage collection immediately following line 11.
  6. The Main object from line 4 is first eligible for garbage collection immediately following line 12.




Answer



B, D.

Note

The Main object from line 3 has two references to it: one and three.

The references are nulled out on lines 6 and 8, respectively.

B is correct because this makes the object eligible for garbage collection after line 8.

The Main object from line 4 only has a single reference to it: two.

D is correct because this single reference becomes null on line 9.

The Main object declared on line 10 becomes eligible for garbage collection at the end of the method on line 12.

Calling System.gc() has no effect on eligibility for garbage collection.