Win a copy of Terraform in Action this week in the Cloud 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • Al Hobbs
  • salvin francis

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 ?
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.
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic