Java OCA OCP Practice Question 488

Question

At which line is the Vector object created in line 4 first subject to garbage collection?

1. import java.util.*;  
2. public class Main {  
3.  public static void main(String[] args) {  
4.   Vector v1 = new Vector(); 
5.   Vector v2 = new Vector();  
6.   v1.add("This");  
7.   v1.add(v2);  // w w w .j  a v  a2  s. c o m
8.   String s = (String) v1.elementAt(0);  
9.   v1 = v2;  
10.  v2 = v1;  
11.  v1.add(s);  
12. }  
}  
  • A. Line 5
  • B. Line 6
  • C. Line 8
  • D. Line 9


D.

Note

The object created in line 4 is assigned to the v1 variable up to line 9.

At this point, a new object is assigned to the v1 variable, and the object created in line 4 is no longer accessible to the program.




PreviousNext

Related