Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes MindQ q#36 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 "MindQ q#36" Watch "MindQ q#36" New topic
Author

MindQ q#36

quan zhu
Greenhorn

Joined: Dec 14, 2000
Posts: 27
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 answer is b. Would somebody explain why because I think it is a. I thought there are 3 objects in the String pool here. Since they all have references pointed to them, none will be garbage collected. Thanks
James Baud
Ranch Hand

Joined: Jan 06, 2001
Posts: 60
Objects in the String pool are never gc'd. So only 1 will be eligible for gc. There's lot of discussions on this in JavaRanch if you search for string pool.
My understanding is that this type of question will not appear in the exam, according to this post.

------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)


<B>~James Baud</B><P>He who asks, is a fool for five minutes;<BR>but, he who does not ask, remains a fool forever. (Chinese proverb)
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Quan,
As I see the problem, James comment about String pool doesn't apply to this problem. Lets step through the code to see how come b is the right answer.

Line 1 gives us no new objects. Line 2 gives us the first object. Our memory now looks like this:
"Nick" <-- newName
Line 3 redirects the newName reference to point to another object. Our memory now looks like this:
"Nick"
"Jason" <-- newName
Line 4 finally gives name an object to reference. Our memory now looks like this:
"Nick"
"Jason" <-- newName
"Frieda" <-- name
Line 5 creates another reference variable and points it to the object already pointed to by name. Our memory now looks like this:
"Nick"
"Jason" <-- newName
"Frieda" <-- name <-- newestName
Line 6 removes the name reference by setting it to null. Our memory now looks like this:
"Nick"
"Jason" <-- newName
"Frieda" <-- newestName
Now we are at the line marked '// Line A'. Objects can only be garbage collected if no references to them exist. How many items in the above list have no references to them?
Learning one step at a time,
Manfred.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The issue of whether String Literals can be GC'd is being discussed here. http://www.javaranch.com/ubb/Forum24/HTML/008746.html
I believe that the current answer is that it is left up to the vendor to decide if they want to do it. If they DO do it, it is not until the class is unloaded which would presumably be past line A.
Based on that, the conclusion is that this is a poor choice for a question on GC.


"JavaRanch, where the deer and the Certified play" - David O'Meara
quan zhu
Greenhorn

Joined: Dec 14, 2000
Posts: 27
Thank you all. That is clear.
 
Consider Paul's rocket mass heater.
 
subject: MindQ q#36
 
Similar Threads
Eligible for GC
MindQ #36
dout regarding garbage collection
garbage collection
gc ??