Monday 18 March 2013

SCJP - 35 -Given Example


At what point is only a single object eligible for GC?

A. After line 8 runs.
B. After line 9 runs.
C. After line 10 runs.
D. After line 11 runs.
E. Compilation fails.
F. Never in this program.
G. An exception is thrown at runtime.

Answer:

G is correct. An error at line 10 causes a NullPointerException to be thrown because
e2 was set to null in line 8. If line 10 was moved between lines 7 and 8, then F would be
correct, because until the last reference is nulled none of the objects is eligible, and once
the last reference is nulled, all three are eligible.

A, B, C, D, E, and F are incorrect based on the above. (Objective 7.4)
==
class Eco {
public static void main(String[] args) {
Eco e1 = new Eco();
Eco e2 = new Eco();
Eco e3 = new Eco();
e3.e = e2;
e1.e = e3;
e2 = null;
e3 = null;
e2.e = e1;
e1 = null;
1}
Eco e;
}


No comments:

Post a Comment