Java OCA OCP Practice Question 162

Question

Given:

public class Main { 
  int id; // w w w.  j  a v  a2s  .  com
  Main(int i) { id = i; } 
  public static void main(String[] args) { 
    new Main(3).go(); 
    // commented line 
  } 
  void go() { 
    Main w1 = new Main(1); 
    Main w2 = new Main(2); 
    System.out.println(w1.id + " " + w2.id); 
  } 
} 

When execution reaches the commented line, which are true?

Choose all that apply.

  • A. The output contains 1
  • B. The output contains 2
  • C. The output contains 3
  • D. Zero objects are eligible for garbage collection
  • E. One object is eligible for garbage collection
  • F. Two objects are eligible for garbage collection
  • G. Three objects are eligible for garbage collection


A, B, and G are correct.

Note

The constructor sets the value of id for w1 and w2.

When the commented line is reached, none of the three Main objects can be accessed, so they are eligible to be garbage collected.




PreviousNext

Related