File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage from MindQ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage from MindQ" Watch "Garbage from MindQ" New topic

Garbage from MindQ

Ranch Hand

Joined: Feb 21, 2001
Posts: 63
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
a) 0
b) 1
c) 2
d) 3
e) 4

The Answer b .
Can any one explain me how . This takes a lot of time...

Think Big . So shall you become big.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In my opinion the answer is '0' because all are string literals here and String literals are not garbage collected.
Do correct me if I am wrong.
Cristian Negresco
Ranch Hand

Joined: Sep 15, 2001
Posts: 182
I think too that the correct answer is 0.
Only to be sure, check if these are all the lines, sometimes there is one GC object, and this is the one that executes the code.
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Answer is definitely 0. For the n-th time, String literals are NOT garbage collected
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform

[Blog] [Blogroll] [My Reviews] My Linked In
sridevi kumar
Ranch Hand

Joined: Oct 17, 2001
Posts: 31
This contradicts with tutorial "Classes in Java":
public class GCTest {
public static void main(String args[]) {
String a,b;
String c = "test";
a = c;
c = null; // The String "test" is not yet
//available for GC as a still points to "test"
b = new String("xyz");
b = c; // String "xyz" is now available for GC.
a = null;
//String "test" is now available for GC.
This explanation with yours confuses me. Could someone explain the correct procedure in detail?
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
Originally posted by Valentin Crettaz:
Answer is definitely 0. For the n-th time, [b]String literals are NOT garbage collected

Im confused too.
String literals never get collected by the GC Thread, but y question is : are they "elegible" ???
Im agree with you Valentin, but Im a little confused about the reason why String literals are never collected...
Ive just read the topic:
Topic: Are string literals garbage collected?
And I have it clear now
[This message has been edited by Zkr Ryz (edited November 23, 2001).]
I agree. Here's the link:
subject: Garbage from MindQ
It's not a secret anymore!