• 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

Garbage Collection in disguise

 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this question:
Assume that MyClass is a class which defines two methods f() and g().
How many objects would be eligible for garbage collection by the time the method g() is invoked

MyClass c1=new MyClass();
MyClass c2=new MyClass();
f(c1);
c1=null;
c2=c1;
g();

A 1
B 2
C 0
D The given information is insufficient.

First I answered it D but then thought that atleast one eligible object is visible here in this code ,ie. c2 since it has been assigned null.

But the anwers is actually D.
I know its not wrong. But even A is not wrong. So how should we answer such questions?

Thanks
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you noticed, it could be one object, possibly two objects, that is/are eligible for garbage collection. So D is correct; there's not enough information to decide. Note that the question is asking for a definite number of objects, not "at least" how many - so A,B,C are definitely wrong.
reply
    Bookmark Topic Watch Topic
  • New Topic