OCA Java SE 8 Mock Exam - OCA Mock Question 2








Question

Which are true of the following code? (Choose all that apply)

     1:import java.util.*; 
     2:public class Rectangle { 
     3:  public Rectangle(String n) { 
     4:     name = n; 
     5:  } 
     6:  public static void main(String[] args) { 
     7:    Rectangle one = new Rectangle("g1"); 
     8:    Rectangle two = new Rectangle("g2"); 
     9:    one = two; 
    10:   two = null; 
    11:   one = null; 
    12: } 
    13:   private String name; 
    14:} 

  1. Immediately after line 9, no Rectangle objects are eligible for garbage collection.
  2. Immediately after line 10, no Rectangle objects are eligible for garbage collection.
  3. Immediately after line 9, only one Rectangle object is eligible for garbage collection.
  4. Immediately after line 10, only one Rectangle object is eligible for garbage collection.
  5. Immediately after line 11, only one Rectangle object is eligible for garbage collection.
  6. The code compiles.
  7. The code does not compile.




Answer



C, D, F.

Note

The code above compiles without issues. So G is not correct. F is correct.

Immediately after line 9, only Rectangle g1 is eligible for garbage collection since both one and two point to Rectangle g2.

Immediately after line 10, there still only Rectangle g1 is eligible for garbage collection. Reference one points to g1 and reference two is null.

Immediately after line 11, both Rectangle objects are eligible for garbage collection since both one and two point to null.