Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exam question regarding garbage collection

Status
Not open for further replies.

thelordoftherings

Programmer
May 16, 2004
616
IL
Hello,

I've encountered with this question in a certain exam:
Given:
10. public Object m() {
11. Object o = new Float(3.14F);
12. Object [] oa = new Object[1];
13. oa[0] = o;
14. o = null;
15. return oa[0];
16. }
When is the Float object, created in line 11, eligible for garbage collection?
A. Just after line 13.
B. Just after line 14.
C. Never in this method.
D. Just after line 15 (that is, as the method returns).

The correct answer is: B.
And my question is why? oa[0] still holds reference to this Object and it returns it to the method caller, so if there is still live regerence to this Object how can it be eligable for garbage collection?
 
... and the question says
When is the Float object, created in line 11, eligible for garbage collection

which would be the Float wrapper object created by the new keyword. Not the object reference o.

Tim
 
I think that's a matter of interpretation, hence the ilogical answer at first sight.

Cheers,
Dian
 
Normally I'm not a language-lawyer.
And in everyday-coding-live, I use a somewhat relaxed language, when talking or thinking about code.

But when we want to know the plain truth, I guess we have to realize, that there isn't an o-object.
There is a Float-object, which is referenced, once by o, once by oa[0].
Are references eligible for garbagecollection at all?
I'm not sure, but I don't think so.

seeking a job as java-programmer in Berlin:
 
I think I agree, Stefan. The references are special. You can't treat them as objects and you can't 'reference-a-reference' like you'd have 'pointers-to-pointers' in C.

For me, the OP's exam question is wrong.

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top