This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the output for this - give explanation

 
nimish kumar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi nimish, welcome to javaranch.

Nimish, when you post a question from any mock exam, you have to Quote Your Sources ...
 
nimish kumar
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Ankit. Ya I will take care of this in future posts.

The source of above question -
Mock exam on scjptest.com
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic