• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

q on garbage collection

 
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic