aspose file tools*
The moose likes Mock Exam Errata and the fly likes MindQ question 36 garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "MindQ question 36 garbage collection" Watch "MindQ question 36 garbage collection" New topic
Author

MindQ question 36 garbage collection

mary lee
Greenhorn

Joined: Apr 03, 2001
Posts: 8
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
the answer is 1
but I think should be 2, there are three String objects "Nick"
"Jason" and "Frieda", only "Frieda" is still referrenced by newestName , other two are not referrenced any more
is it right?
could anyone help?

bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Jason is referenced by newName
Frieda is referenced by newestName
Nick isn't referenced at all
So only 1 object is eligible
Hope that helps,
Bill
Haining Mu
Ranch Hand

Joined: Jun 01, 2001
Posts: 51
Originally posted by bill bozeman:
Jason is referenced by newName
Frieda is referenced by newestName
Nick isn't referenced at all
So only 1 object is eligible

I'm still wondering the answer or question wording. Since all strings here are literals, NOT initialized using new op., there is no way we can say how many are eligible for gc.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Haining,
What you say is true but for the purpose of answering mock exam questions that use String literals, assume they are garbage collected.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Muhammad Farooq
Ranch Hand

Joined: May 08, 2001
Posts: 356
The question says, "How many objects are eligible for gc?". I think when name =null, it means the object "name" as well as "newestName" (which is also assigned to name) is eligible for gc. I think, here, two objects are eligible for gc.
Please correct me if I am wrong........


Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Muhammad,
"name" is a reference variable, not an object.
Java has two types of variables: primitive type and reference type.
A primitive type variable ie <code>int i</code> actually contains the value that is assigned to it.
<pre>
Memory
---------
int i = 10 -> | 10 |
---------
</pre>
A reference type variable ie <code>String name</code> contains the memory address for the object that is assigned to it; not the object itself.
<pre>
Memory Memory
----------- ------------
String str = "Hello" -> | address | points to | Object |
----------- ------------
</pre>
Only the memory where objects are stored is garbage collect. The memory area containing variables is not.
Hope that helps.

Sorry ... haven't mastered ASCII art hope you get the idea.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited June 20, 2001).]
[This message has been edited by Jane Griscti (edited June 20, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MindQ question 36 garbage collection