File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is the output for this - give explanation

 
nimish kumar
Ranch Hand
Posts: 60
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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: 9496
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • 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
  • 0
  • 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