Machine-translated by Google. Questions are authored in English.
Top

Discussion

class X2 
{
    public X2 x;
    public static void main(String [] args) 
    {
        X2 x2 = new X2();  /* Line 6 */
        X2 x3 = new X2();  /* Line 7 */
        x2.x = x3;
        x3.x = x2;
        x2 = new X2();
        x3 = x2; /* Line 11 */
        doComplexStuff();
    }
}

after line 11 runs, how many objects are eligible for garbage collection?

  • A.1
  • B.2
  • C.3

Answer: B

Correct answer: 2

After line 11 both x2 and x3 point at the object created on line 10, so the two objects made on lines 6 and 7 have no reference from any live variable. They still reference each other through their x fields, but mutual reference is not reachability - a modern collector traces from the roots rather than counting references, so an isolated island of objects pointing at one another is collected. Two objects are therefore eligible.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.