Java OCA OCP Practice Question 170

Question

Given:

3. class B { } //from w ww.  j av a2  s .c  o  m
4. class A { 
5.   static B b1; 
6.   B b2; 
7. } 
8. public class Main { 
9.   public static void main(String[] args) { 
10.     B b1 = new B();     B b2 = new B(); 
11.     A a1 = new A();   A a2 = new A(); 
12.     a1.b1 = b1; 
13.     a1.b2 = b1; 
14.     a2.b2 = b2; 
15.     a1 = null;  b1 = null;  b2 = null; 
16.     // do stuff 
17.   } 
18. } 

When line 16 is reached, how many objects will be eligible for garbage collection?

  • A. 0
  • B. 1
  • C. 2
  • D. 3
  • E. 4
  • F. 5


B is correct.

Note

There is still a reference to the object referred to by a2, and that there is still a reference to the object referred to by a2.b2.

You can still access the other B object through the static variable a2.b1 because it's static.




PreviousNext

Related