After line 6 runs. how many objects are eligible for garbage collection? A. 0 B. 1 C. 2 D. 3 E. 4 Answer given 10. B. By the time line 6 has run, the only object without a reference is the one generated as a result of line 4. Remember that “Java is pass by value,” so the reference variable x is not affected by the m1() method.
My answer is C. I completely agree with Kathy's answer and explanation given above. But what about the object created in the method m1()? Isn't that also eligible for GC as no thread can reach it? I would like to hear from Kathy. Thanks in advance, Sarma
But what about the object created in the method m1()? Isn't that also eligible for GC as no thread can reach it?
Sarma, Infact, thats the only object eligible for GC. According to the explanation, if you closely observe, the only object without a reference is the one generated. This object is generated in the m1() method and its reference is passed as a return reference variable to the line 4. Apart from this object , there is no new object being created at line 4 Hope this helps Sri