File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Taking out the Garbage! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Taking out the Garbage!" Watch "Taking out the Garbage!" New topic
Author

Taking out the Garbage!

Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1194
This is from Whizlabs tutorial for the certificaton exam. I don't follow the explanation for the sample code. To me, there should be 1 object available for gc after line 4. Here's the code:
Garbage collection
Exercise page 5 of 5

Question:

How many objects will be eligible for garbage collection after line 7?

Choices:

A. 0
B. 1
C. 2
D. 3
E. Code does not compile
Correct choice:

B
Explanation:

Among the three objects created in lines 1, 2, and 3, only the Integer object is eligible for garbage collection at the end of line 7. The reference variable a, which originally referred to the Integer object is made to refer to the String object in line 5. So the Integer object is eligible for garbage collection after line 5, since there are no variables referring to it. The variables b and c refer to the String and Long objects in lines 6 and 7, so they are not eligible for garbage collection.

I thought that the Integer object would be available after line 4, when a is set to null. Am I wrong, or is the answer wrong?


[ January 08, 2005: Message edited by: Barry Gaunt ]
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
You are 100% correct


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"I thought that the Integer object would be available after line 4, when a is set to null."

Yes, this is true. So it must be still garbage collectable after line 7 (if it has not been already collected). The other three objects the String literal "100" and the Long(100) and String("100") are still referred to. So the answer is correct (1 object) but their explanation has a glitch.
[ January 08, 2005: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Taking out the Garbage!
 
Similar Threads
garbage collection
eligible for garbage collection
Do Exceptions thrown are eligible for Garbage Collection?
Mock exam questions section 3
Please solve the following Problem.