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. }
A. 0
B. 1
C. 2
D. 3
E. 4
F. 6
Answer:E
Icelandic i3 = new Icelandic();
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. }
A. 0
B. 1
C. 2
D. 3
E. 4
F. 6
Answer:E
Explanation:
We use diagrammatic approach for your better understanding.Please read the question carefully before tracing the images below.
Initially when three Icelandic are objects are created using reference variable i1,i2,i3 using the statements
Icelandic i1 = new Icelandic();
Icelandic i2 = new Icelandic();Icelandic i3 = new Icelandic();
Remember Long weight is a primitive type of object which is within Icelandic object. i1 is pointing to Object Icelandic A with weight object of Long type .Therefore Totally 2 Objects for i1 .Similarly 2 Objects for i2 and 2 Objects for i3. And also remember that single reference variable can't point to 2 objects but here in this case Long Weight object is a primitive data member of Icelandic Object.
After i3 = i1
After i1 = i2
After i2 = NULL
Since i1 pointing to Icelandic B ,when we write i3 =i1 - i3 is made to point to Icelandic B .
After i3 = i1
Therefore Icelandic A along with its Long weight object and Icelandic C along with its Long weight Object is eligible for Garbage Collection .Total 4 Objects are eligible for Garbage Collection.Option E.
0 comments:
Post a Comment