• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SC ExamLab (Diagnostic). Question 70

 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

Have a look at the following post:
Q on GC from Exam Lab
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21015
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Edmen Tay
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,
Got it.
Thanks for the clarification.

Regards
Edmen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic