Garbage Collections

public class X 
    public static void main(String [] args) 
        X x = new X();
        X x2 = m1(x); /* Line 6 */
        X x4 = new X();
        x2 = x4; /* Line 8 */
    static X m1(X mx) 
        mx = new X();
        return mx;
After line 8 runs. how many objects are eligible for garbage collection?

A 2

B 1

C 3

D 0

ANS:A - 0

By the time line 8 has run, the only object without a reference is the one generated as a result of line 6. Remember that 'Java is pass by value,' so the reference variable x is not affected by the m1() method. Ref:

img not found

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.