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 q on garbage collection 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 "q on garbage collection" Watch "q on garbage collection" New topic

q on garbage collection

JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
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

I answered 0 but the correct answer is one.
I answered 0, bcos string1 is set to null, but in the very next line, is says string1 = string2.

plz explain.

SCJP 1.4, SCWCD 1.4<br /> <br />Thanks in advance!<br />Jayashree.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Jayashree, please quote the exact source of these questions. This one is clearly wrong.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
John Neel

Joined: Jul 20, 2004
Posts: 6
i feel the answer 1 is correct.

as the object "Test" is not referred by any one, it can be garbage collected.

plz correct me if i were wrong
sandu sudhakar

Joined: Nov 17, 2004
Posts: 4
Ref: article

"String Literals are not eligible for garbage collection".

So in this example, none of the objects are eligible for garbage collection.
Answer: 0
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
From Bert Bates (who should know):
focus on non-String objects when you're studying GC, don't worry about GC and Strings together.

This is a problem with some mock exams, they have clever questions that are outside the exact scope of the exam.

Mike Gershman
SCJP 1.4, SCWCD in process
I agree. Here's the link:
subject: q on garbage collection
jQuery in Action, 3rd edition