Poll: Kiểm tra kiến thức Core Java (Garbage Collection )- 1.07

When is the Demo object eligible for garbage collection?

class Test 
{  
    private Demo d; 
    void start() 
    {  
        d = new Demo(); 
        this.takeDemo(d); /* Line 7 */
    } /* Line 8 */
    void takeDemo(Demo demo) 
    { 
        demo = null;  
        demo = new Demo(); 
    } 
}
  • A After line 7
  • B After line 8
  • C After the start() method completes
  • D When the instance running this code is made eligible for garbage collection.

0 voters

Answer:

Option D

[details=Explanation:]Option D is correct. By a process of elimination.

Option A is wrong. The variable d is a member of the Test class and is never directly set to null.

Option B is wrong. A copy of the variable d is set to null and not the actual variable d.

Option C is wrong. The variable d exists outside the start() method (it is a class member). So, when the start() method finishes the variable d still holds a reference.[/details]

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?