When does the Float object created on line 3 become eligible for garbage collection? public Object m() { Object o = new Float(3.14F); Object[] oa = new Object[1]; oa[0] = o; // line 5 o = null; // line 6 oa[0] = null; // line 7 return o; // line 8 }
-
Ajust after line 5
-
Bjust after line 6
-
Cjust after line 7
-
Djust after line 8
-
Enever
Answer
Correct Answer: just after line 7
Explanation
Introduction / Context:Determining GC eligibility requires tracking every live reference to an object. Arrays are objects too and can keep other objects alive by holding references in their elements.
Given Data / Assumptions:
- A Float is created and referenced by
o. - The reference is stored into
oa[0]. ois later set to null and thenoa[0]is set to null.
Concept / Approach:The Float remains reachable as long as either o or oa[0] holds its reference. It becomes eligible only when both references are removed. Returning o at line 8 returns null (since line 6 set it to null) and thus does not resurrect the object.
Step-by-Step Solution:
After line 5: two references exist (viao and oa[0]).After line 6: one reference remains (via oa[0]).After line 7: no references remain; the Float is eligible for GC.Line 8 returns o which is null; this does not affect eligibility.Verification / Alternative check:Swap the order of lines 6 and 7; eligibility would still occur after the second nulling step.
Why Other Options Are Wrong:Lines 5 and 6 still leave a live reference; line 8 returns null, not the Float.
Common Pitfalls:Overlooking references held inside arrays; assuming returning o implies returning the object (it returns null in this code).
Final Answer:just after line 7