This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SC ExamLab (Diagnostic). Question 70 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SC ExamLab (Diagnostic). Question 70" Watch "SC ExamLab (Diagnostic). Question 70" New topic
Author

SC ExamLab (Diagnostic). Question 70

Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
public class A{
public static void main(String[] args) {
test a = new test("obj1");
test b = new test("obj2");
test c = new test("obj3");
a.aob = b;
b.aob = a;
c.aob = a.aob;
test d=new test("obj4").aob = new test("obj5");
c=b;
c.aob = null;
System.gc();

}
}
Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
Hi all, please ignore the first post. I had accidentally keyed wrongly. Here i am,

public class A{
public static void main(String[] args) {
A a = new A();
A b = new A();
A c = A();
a.aob = b; //line 6
b.aob = a;//line 7
c.aob = a.aob; //line 8
A d=new A().aob = new A(); //line 9
c=b; //line 10
c.aob = null; //line 11
System.gc();
}
}

How many objects are eligible for GC after executing statement "c.aob=null"?

The answer is 2.

Here my drawing,

a -> A (obj1)
b -> A (obj2)
c -> A (obj3)

then
a -> A (obj1) -> A (obj2) //line 6
b -> A (obj2) -> A (obj1) //line 7
c -> A (obj3) -> A (obj2) //line 8

d -> A (obj4) -> A (obj5) //line 9

c -> A (obj2) //line 10, obj3 GCed here!!

c -> A (obj2) -> null //line 11

Where is another object??

Please help..

Regards,
Edmen
M Srilatha
Ranch Hand

Joined: Aug 27, 2008
Posts: 137
HI,

Have a look at the following post:
Q on GC from Exam Lab


Thanks,<br />Srilatha M
Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
Here is my sample of tracing,

public class test {

test aob;
String sName;

public test(String name){
this.sName = name;
}
public static void main(String[] args) {
test a = new test("obj1");
test b = new test("obj2");
test c = new test("obj3");
a.aob = b;
b.aob = a;
c.aob = a.aob;
test d=new test("obj4").aob = new test("obj5");
c=b;
c.aob = null;
System.gc();

}
public void finalize(){
System.out.println("finalize called for: "+sName);
}
}

The output is :
finalize called for: obj4
finalize called for: obj3

I am puzzling with obj4. Anyone know? Kindly please help.

Thanks

Regards,
Edmen
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18136
    
  39

Pretty good drawings... but you have amistake here....



Keep in mind that the assoc for assigments are right to left, so it is not this...



It is this...



One of the two objects instantiated at line 9 is no longer reachable by the next line.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Edmen Tay
Ranch Hand

Joined: Oct 21, 2008
Posts: 39
Hi Henry,
Got it.
Thanks for the clarification.

Regards
Edmen
 
 
subject: SC ExamLab (Diagnostic). Question 70
 
Similar Threads
Confused about the output(Garbage collection)
Is this legal syntax ? A d = new A().aob = new A();
Garbage Collection
doubt about GC
Problem in Garbage collector