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 Eligible for GC 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 » Programmer Certification (SCJP/OCPJP)
Bookmark "Eligible for GC" Watch "Eligible for GC" New topic
Author

Eligible for GC

Neelesh Bodas
Ranch Hand

Joined: Jul 20, 2006
Posts: 107
This is Q36 in MindQ's SCJP Test.

36. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?




a) 0
b) 1
c) 2
d) 3
e) 4

The answer that they provide is b) 1.

My argument is that since all the assignments are made to the string objects in the string pool rather than heap, none of the objects will be eligible for GC till the class is unloaded. So I prefer to choose (a)

Any opinions?
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

I disagree with that reasoning. If there are no strong references to any of those strings, there is no need to keep them interned.
Amit Batra
Ranch Hand

Joined: Mar 04, 2006
Posts: 361
Dont worry I dont think questions like these are going to be on the exam, Mr. Bert Bates said so himself not too long ago.
And you know what that means...This would also make a great interview questions
Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
Mr. C Lamont Gilbert
If there are no strong references to any of those strings, there is no need to keep them interned.


I agree with Mr Lamont.
Neelesh:
Since String literal pool also contains objects(of strings), there is no reason why they cannot be garbage collected if there are no strong references ?
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Strings, Literally

Unlike most objects, String literals always have a reference to them from the String Literal Pool. That means that they always have a reference to them and are, therefore, not eligible for garbage collection.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Eligible for GC