this question is like my previous one
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 14 is reached, how many objects are eligible for the garbage collector?
A. 0
B. 1
C. 2
D. 3
E. 4
F. 6
I chose A but the right answer is E, but I don’t know Why?
Let’s call the three
Icelandicobjects created inmainasA,BandC.Initialy
i1=A,i2=Bandi3=C;After
i3 = i1i1=A,i2=Bandi3=A;After
i1 = i2i1=B,i2=Bandi3=A;After
i2 = null:i1=B,i2=nullandi3=A;After
i3 = i1i1=B,i2=nullandi3=BIn line 14, there are standing references to only
Bobject of typeIcelandic.AandCare lost in the running program.Each
Icelandicobject that is lost gives garbage collector two objects to collect, ie. theIcelandicobject itself and theLongobject within everyIcelandic, which make the total number of garbage collected objects 4.Since
makeNoisemethods are never called, they do not change the outcome.