Java OCA OCP Practice Question 3046

Question

Given:

1. import java.util.*;  
2. public class Main {  
3.   static List<Exception> me;  
4.   Exception myEx;  /*from  ww  w  .ja  va2 s .co  m*/
5.   public static void main(String[] args) {   
6.     Main[] aa = {new Main(), new Main()};  
7.     me = new ArrayList<Exception>();  
8.     for(int i = 0; i < 2; i++) {  
9.       try {  
10.         if(i == 0) throw new Exception();  
11.         if(i == 1) throw new MyException();  
12.       }  
13.       catch(Exception e) {  
14.         me.add(e);  
15.         aa[i].myEx = e;  
16.       }  
17.     }  
18.     System.out.println(aa.length + " " + me.size());  
19.     aa = null; me = null;  
20.     // do more stuff  
21. } }  
22. class MyException extends Exception { } 

When line 20 is reached, how many objects are eligible for garbage collection?

  • A. 2
  • B. 4
  • C. 5
  • D. 6
  • E. 7
  • F. 8


D is correct.

Note

Line 6 creates three objects: an array and two Main objects.

Line 7 creates an ArrayList object.

Line 10 and 11 each create some sort of Exception object.

While it's true that each Main also has a reference to an Exception object, those references are referring to the same exceptions that are in the array.




PreviousNext

Related