Java OCA OCP Practice Question 2845

Question

Given:

3. public class Main {   
4.   static Main base;  
5.   Main f;  /*from w ww. j  a v  a2  s.  c  o m*/
6.   public static void main(String[] args) {  
7.     new Main().go();  
8.     // do more stuff  
9.   }  
10.   void go() {  
11.     Main f1 = new Main();  
12.     base = f1;  
13.     Main f2 = new Main();  
14.     f1.f = f2;  
15.     Main f3 = f1.f;  
16.     f2.f = f1;  
17.     base = null; f1 = null; f2 = null;  
18.     // do stuff  
19. } } 

Which are true? (Choose all that apply.).

  • A. At line 8, one object is eligible for garbage collection.
  • B. At line 8, two objects are eligible for garbage collection.
  • C. At line 8, three objects are eligible for garbage collection.
  • D. At line 18, 0 objects are eligible for garbage collection.
  • E. At line 18, two objects are eligible for garbage collection.
  • F. At line 18, three objects are eligible for garbage collection.


C and D are correct.

Note

A total of three Main objects are created.

At line 18, the anonymous (main()'s), Main can still be accessed via "this", and the other two Main objects can be accessed via f3 (f3 and f3.f).

At line 8, none of the three Main objects are accessible.




PreviousNext

Related