how many objects are eligible for the garbage collector?

Given:

3. interface Animal { void makeNoise(); }
4. class Horse implements Animal {
5. Long weight = 1200L;
6. public void makeNoise() { System.out.println(“whinny”); }
7. }
8. public class Icelandic extends Horse {
9. public void makeNoise() { System.out.println(“vinny”); }
10. public static void main(String[] args) {
11. Icelandic i1 = new Icelandic();
12. Icelandic i2 = new Icelandic();
13. Icelandic i3 = new Icelandic();
14. i3 = i1; i1 = i2; i2 = null; i3 = i1;
15. }
16. }

When line 15 is reached, how many objects are eligible for the garbage collector?

Given:

3. interface Animal { void makeNoise(); }
4. class Horse implements Animal {
5. Long weight = 1200L;
6. public void makeNoise() { System.out.println(“whinny”); }
7. }
8. public class Icelandic extends Horse {
9. public void makeNoise() { System.out.println(“vinny”); }
10. public static void main(String[] args) {
11. Icelandic i1 = new Icelandic();
12. Icelandic i2 = new Icelandic();
13. Icelandic i3 = new Icelandic();
14. i3 = i1; i1 = i2; i2 = null; i3 = i1;
15. }
16. }

When line 15 is reached, how many objects are eligible for the garbage collector?

A.
0

B.
1

C.
2

D.
3

E.
4

F.
6



Leave a Reply 3

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


Nikita Gupta

Nikita Gupta

Hey anyone can exxplain me,why 4 objects are deleted?

Regina

Regina

Each Icelandic also has a Long object (the weight). When i3 gets set to i1, the reference to i3 and its weight is lost, so 2 objects are eligible for GC. Then i2 gets set to null, so 2 more objects are eligible for GC.

Matti

Matti

E is correct.
Because each Icelandic has a Long object, so that if GC takes objects i1 and i3 away, then 4 objects are eligible for the garbage collector in this case!