| Author |
what is the output for this - give explanation
|
nimish kumar
Ranch Hand
Joined: Dec 04, 2009
Posts: 54
|
|
What is the output of below sample code.
Please give explanation. What I feel is that it should be option E, but the answer is option A.
Can anyone explain
What is true about objects referenced by a, b, aa at the line labeled "// some code goes here"?
class A {
private B b;
public A() {
this.b = new B(this);
}
}
class B {
private A a;
public B(A a) {
this.a = a;
}
}
public class Test {
public static void main(String args[]) {
A aa = new A();
aa = null;
// some code goes here
}
}
A) The objects referenced by a and b are eligible for garbage collection.
B) None of these objects are eligible for garbage collection.
C) Only the object referenced by "a" is eligible for garbage collection.
D) Only the object referenced by "b" is eligible for garbage collection.
E) Only the object referenced by "aa" is eligible for garbage collection.
|
 |
Ankur kothari
Ranch Hand
Joined: Sep 06, 2009
Posts: 531
|
|
|
i think the objects referenced by aa,a(aa and a refer to the same object) and b are eligible for gc
|
 |
P Teng
Greenhorn
Joined: Nov 20, 2009
Posts: 16
|
|
|
the answer is A because when aa=null eecutes, there is no outside world referene that can help reach to objets of class A or B. Hence all objects are eligible for GC
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
Hi nimish, welcome to javaranch.
Nimish, when you post a question from any mock exam, you have to Quote Your Sources ...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
nimish kumar
Ranch Hand
Joined: Dec 04, 2009
Posts: 54
|
|
thanks Ankit. Ya I will take care of this in future posts.
The source of above question -
Mock exam on scjptest.com
|
 |
 |
|
|
subject: what is the output for this - give explanation
|
|
|