1. class Bar { } 
1. class Test { 
2. Bar doBar() { 
3. Bar b = new Bar(); 
4. return b; 
5. } 
6. public static void main (String args[]) { 
7. Test t = new Test(); 
8. Bar newBar = t.doBar(); 
9. System.out.println(“newBar”); 
10. newBar = new Bar(); 
11. System.out.println(“finishing”); 
12. } 
13. } At what point is the Bar object, created on line 3, eligible for garbage collection? 
A. After line 8. 
B. After line 10. 
C. After line 4, when doBar() completes. 
D. After line 11, when main() completes. 
12. From the following code how many
objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
大家各抒已见,选什么,为什么要选它??

解决方案 »

  1.   

    1. A
    因为只在第8行的时候调用了doBar()方法.
    2. B
    没有出现new 关键字,只有两个字符串常量
      

  2.   

    同意楼上的String a="s"只是定义了变量而没有创建对象,new才是创建对象
      

  3.   

    第一个是 A吧
    垃圾回收就好像地心引力一样,只要没有对象再引用它,它就会被gc吸走。第二个。。我觉得是A
    前面几位说的没错,String a="s"的情况并没有真正的创建对象。但是实际上是在运行栈中查找是否已经有指向
    一个s的引用,如果有就复制这个引用。
    所以题目当中 "Test"已经没有引用指向它了,被回收
    而"Today"有两个引用指向,不会被回收。初学而已,仅供参考