Multiple choice

void start() {
A a = new A(); B b = new B(); a.s(b);
b = null; /* Line 5 / a = null; / Line 6 / System.out.println("start completed"); / Line 7 */ } When is the B object, created in line 3, eligible for garbage collection?

  1. after line 5

  2. after line 6

  3. after line 7

  4. There is no way to be absolutely certain.

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

In Java, objects become eligible for garbage collection when they are no longer reachable through any reference chain. After line 5 (b = null), object B is not directly reachable. However, if the call a.s(b) on line 4 stored a reference to b inside object a, then B would still be reachable through a until line 6. Without seeing the code for class A and method s(), we cannot be certain whether B is referenced internally by A. Therefore, absolute certainty is impossible.