Menu
Topics Index
...
`

Garbage Collection


If you need explanation Read this topic

If you need Answer Take test on this topic

Garbage Collection
public class NullObjects {
    public static void main(String[] args) {
        X x = new X();
        Y y = new Y();
        x.y = y;
        /* LINE A */
        Z z = new Z();
        y.z = z;
        z.x = x;
        /* LINE B */
        y = null;
        z = null;
        /* LINE C */
        z = new Z();
        x.y = new Y();
        z = null;
        /* LINE D */
    }
}
class X {
    Y y;
}
class Y {
    Z z;
}
class Z {
    X x;
}
What are the number of objects which are available for garbage collection at the given lines?
A. A=2, B=3, C=3, D=5
B. A=0, B=0, C=0, D=1
C. A=0, B=0, C=0, D=3
D. A=0, B=0, C=2, D=3
E. Compilation Error or Runtime Error
Topic: Other Advanced Topics In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

C might be d answer.only D contains 3 the rest 0.

Posted by Shabaaz Shaikz    2013-11-22 12:46:13


C is the answer.. as at point A two objects r created n addresses of both objects r hold in x and y respectively..at point B totally 3 objects r created n addresses r hold in x , y, z respectively.. at point C though y and z are set to null we can access the objects of y and z using x.y and x.y.z so no object is eligible for garbage collection..at point D only object of X and newly created object of Y are accesible throgh x and x.y and the rest (previous y obj,prev z object and and new z object ) cant be accesed..so 3 objs r eligible for garbage collection

Posted by Pramod Jain    2013-11-22 12:58:49


Congratulations Pramod Jain. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2013-11-25 03:34:21


thank u mc :) got rc :) :)

Posted by Pramod Jain    2013-12-03 04:34:40


© meritcampus 2019

All Rights Reserved.

Open In App