This week's book giveaway is in the General Computing forum.
We're giving away four copies of Learning Regular Expressions and have Ben Forta on-line!
See this thread for details.
Win a copy of Learning Regular Expressions this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

q on garbage collection  RSS feed

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayashree, please quote the exact source of these questions. This one is clearly wrong.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ref: article
http://www.javaranch.com/journal/200409/Journal200409.jsp#a1

"String Literals are not eligible for garbage collection".

So in this example, none of the objects are eligible for garbage collection.
Answer: 0
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!