• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions abt Garbage collection

 
Sanjeet Karamchandani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
These Q r from MindQ #36 & 37
36. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?
String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";
String newestName = name;
name = null;
//Line A
a) 0
b) 1
c) 2
d) 3
e) 4
The ans given is b)1 but i feel it should be c)2 coz when string newName is reinitilized to "jason", the previous string "Nick" is eligible for GC and when name=null; the string "Frieda" is also eligible for GC. This means there r 2 objects eligible for GC and not 1. Anything wrong?
37. Which of the following statements about Java's garbage collection are true?
a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.
Ths ans is a,b,c but i feel a is not correct coz nobody can invoke a GC and one can't be sure whether GC will ever run.
Thanks
Sanjeet
 
Gopinath Rajgopal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q36. -> The answer b)1 is correct.

"........coz when string newName is reinitilized to "jason", the previous string "Nick" is eligible for GC and when name=null; the string "Frieda" is also eligible for GC......."
No, "Frieda" is not eligible for GC cos newestName is referring to it and so it is still reachable !!

Q 37. -> a) is correct cos you are only invoking (requesting) GC to run. The option doesn't say that it will run GC.
 
Gopinath Rajgopal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Btw, what exam is this MindQ? Is it available thru the mock exams page on JavaRanch??
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.to get MindQ mock exam, visit www.javaranch.com/maha and go to the mock exam list.
2. If a string object is created like
String s="sssss";
it is created in the string pool (not in the heap), and not subject to garbage collection.

[This message has been edited by vasansrini (edited August 06, 2000).]
 
Gopinath Rajgopal
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"String s="sssss";
it is created in the heap (not in the string pool), and not subject to garbage collection."

I think it is the opposite...it is created in the string pool and not in the heap........
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are right, corrected my post.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic