wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Questions abt Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Questions abt Garbage collection" Watch "Questions abt Garbage collection" New topic
Author

Questions abt Garbage collection

Sanjeet Karamchandani
Ranch Hand

Joined: Jul 29, 2000
Posts: 30
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

Joined: Jun 26, 2000
Posts: 31
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

Joined: Jun 26, 2000
Posts: 31
Btw, what exam is this MindQ? Is it available thru the mock exams page on JavaRanch??
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Jun 26, 2000
Posts: 31
"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

Joined: Nov 22, 2008
Posts: 18944
you are right, corrected my post.
 
GeeCON Prague 2014
 
subject: Questions abt Garbage collection