Meaningless Drivel is fun!*
The moose likes Mock Exam Errata and the fly likes how many objects are eligible for garbage collection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "how many objects are eligible for garbage collection?" Watch "how many objects are eligible for garbage collection?" New topic
Author

how many objects are eligible for garbage collection?

william tham
Greenhorn

Joined: Aug 10, 2001
Posts: 6
From the following code how many objects are eligible for garbage collection?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
A) 1
B) 2
C) 3
D) 0
Percy Densmore
Ranch Hand

Joined: Mar 06, 2001
Posts: 214
"Test" is the only object that is not being referenced after execution. Both are currently pointing to "Today". So my answer would be: A)1
Percy
Jimmy Blakely
Ranch Hand

Joined: Jul 10, 2001
Posts: 57
The answer is 0.
String literals are not garbage collected.
However if the code was written like this....
String string1 = new String("Test");
String string2 = new String("Today");
string1 = null;
string1 = string2;
The answer would be 1.
Percy Densmore
Ranch Hand

Joined: Mar 06, 2001
Posts: 214
Thanks for the clarification, Jimmy. I knew better

------------------
Percy Densmore
-SCJP2 Die Hard Student
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Percy Densmore you answer is correct. "test" has not be reffernce after the first execution, therefore it is eligible for garbage collection. a. 1 "is the answer"
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I have to agree with Jimmy.
The J2SE API spec for the String.intern() method states "A pool of strings, initially empty, is maintained privately by the class String." Additionally, "All literal strings and string-valued constant expressions are interned," i.e. they are added to the String pool.
This implies that the String class manages "Test" and "Today" within its private String literal pool. Hence, even if "Test" is no longer referenced, the pool manager may still be holding on to it, thereby preventing the garbage collector from freeing the object from memory.
kishor rao
Ranch Hand

Joined: Dec 10, 2002
Posts: 35
when strings are created with the = operator, they are only stored in the string literal pool. strings in the string literal pool are not eligible for garbage collection
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
I agree with Jimmy: String literals are not garbage collected.
Garbage collection questions involving Strings will not be included on the exam.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Thanks, Jimmy.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
This appears to be a question rather than an indication of errata in some unspecified mock exam. I will move it to the Programmer Certification Study forum.
 
 
subject: how many objects are eligible for garbage collection?