• 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

Ambiguous GC?

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folks,
What is your answer to the following question:
----------------------------------------------------------
From the following code how many objects are garbage collected?
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
1) 1
2) 2
3) 3
4) 0
------------------------------------------------------
I ran across this question in one of the mock exams. The answer it gave is #1, with this explanation, " (string1 = string2) causes the garbage collecing of string1 because a fresh copy of string1 is created and the old one is garbage collected."
However, the question asks "how many objects are to be GC'ed?". Such question seems to implies "NOW"... And that kills me! All we know is that the old string1 is ready for GC when it is set to null.... But that does not mean "IT IS" collected.
What do you think? Isn't the question a bit ambiguous ?
Thanks,
Lam
P.S. maybe this is the case where "IS" is "NOT IS"
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I admit that the question is a bit ambiguous. Whenever I ran across this in mocks it used the term eligible, because the garbage collector is vendor implemented. It should be clear that after this code is run, one object is eligible to be garbage collected.
 
reply
    Bookmark Topic Watch Topic
  • New Topic