When line 11 is reached, how many objects are eligible for garbage collection?

Given:

04. public class Tahiti {
05. Tahiti t;
06.
07. public static void main(String[] args) {
08. Tahiti t = new Tahiti();
09. Tahiti t2 = t.go(t);
10. t2 = null;
11. // more code here
12. }
13.
14. Tahiti go(Tahiti t) {
15. Tahiti t1 = new Tahiti();
16. Tahiti t2 = new Tahiti();
17. t1.t = t2;
18. t2.t = t1;
19. t.t = t2;
20. return t1;
21. }
22. }

When line 11 is reached, how many objects are eligible for garbage collection?

Given:

04. public class Tahiti {
05. Tahiti t;
06.
07. public static void main(String[] args) {
08. Tahiti t = new Tahiti();
09. Tahiti t2 = t.go(t);
10. t2 = null;
11. // more code here
12. }
13.
14. Tahiti go(Tahiti t) {
15. Tahiti t1 = new Tahiti();
16. Tahiti t2 = new Tahiti();
17. t1.t = t2;
18. t2.t = t1;
19. t.t = t2;
20. return t1;
21. }
22. }

When line 11 is reached, how many objects are eligible for garbage collection?

A.
0

B.
1

C.
2

D.
3

E.
Compilation fails.

Explanation:



Leave a Reply 1

Your email address will not be published. Required fields are marked *