This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "garbage collection" Watch "garbage collection" New topic
Author

garbage collection

Sri Enga
Ranch Hand

Joined: Feb 02, 2001
Posts: 51
Question from MindQ
36. How many objects are eligible for garbage collection once execution has reached 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
I thought that the answer should be c)2 , but the answer given is b
newname = "Nick"; becomes eligible for garbage collection once the statement newname = "Jason"; is executed and name = null;
is the second object which is eligible
So how come the answer is b) 1
Thanks,
Srini
a hui
Ranch Hand

Joined: Oct 14, 2000
Posts: 39
I'd think it's a mistake because I also get two objects eligible for GC, "Nick" and "Jason". newName is referencing "Frieda".
Sandeep Nachane
Ranch Hand

Joined: Dec 06, 2000
Posts: 57
I would think the answer (b) given is right.. this is why
You must know two most important points about GC .
(1)Objects created on String pool are not gc'ed even though you assign null to them.
(2) Objects created on heap are the only ones available for gc

Keeping this in mind let us analyze the code

Hence only one object is eligible for gc. That is the one created on heap.
Hope it clears it!!
-Sandeep Nachane

------------------
Visit my homepage at
www.tipsmart.com


<A HREF="http://www.tipsmart.com" TARGET=_blank rel="nofollow">www.tipsmart.com</A>
Sandy Lee
Greenhorn

Joined: Jan 18, 2001
Posts: 18
Actually here is why:
String name;
String newname = "Nick";
newname = "Jason";
/* The object "Nick" can no longerbe referenced.(No variable pointing to "Nick" string as newname now points to "Jason")
Therefore at this point one String object eligible for GC-- "Nick". */
name = "Frieda";
String newestName = name;
/* After the above statement, two handles point to the same object. Both newestName and name variables point to the String "Freida". */
name = null;
/* In the above stmt, the name variable no longer points to the STring "Freida". But the newestName continues to point to the String " Freida" */
// Therefore, only one object is eligible for garbage collection.
//and that is " Nick".
huiying li
Ranch Hand

Joined: Feb 12, 2001
Posts: 68
I agree b is the right answer. Here is some code to test it out.

C:\JavaRanch>java GCTest
they point to the same object
newname = Jason
newestName = Frieda
name = null

SCJP
Yogesh Mhatre
Greenhorn

Joined: Feb 15, 2001
Posts: 20
hi Sandeep,
can pls be more clear what do you mean by string pool and heap
Yogesh
 
jQuery in Action, 2nd edition
 
subject: garbage collection