• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jdiscuss question of the day - April 3, 2005

 
Neelima Chandran
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jdiscuss question of the day - April 3, 2005

Which of these statements are true?

Select 1 correct option.

a) Objects can explicitly be destroyed using the keyword delete.

b) An object will be garbage collected immediately after the last reference
to the object is removed.

c) If object obj1 is accessible from obj2 and obj2 is accessible from obj1,
then obj1 and obj2 are not eligible for garbage collection.

d) Once an object has become eligible for garbage collection, it will remain
eligible until it is destroyed.

e) An object can be finalized only once.

-------------------------------------------------------------

My answer was (c)

The answer given is e) with the explanation
if an object is resurrected in finalize() then it will never be finalized again.

Can somebody elaborate on this please?

Thanks,
Neelima
 
shyam ramineni
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Jdiscuss question of the day - April 3, 2005

Which of these statements are true?

Select 1 correct option.

a) Objects can explicitly be destroyed using the keyword delete.

b) An object will be garbage collected immediately after the last reference
to the object is removed.

c) If object obj1 is accessible from obj2 and obj2 is accessible from obj1,
then obj1 and obj2 are not eligible for garbage collection.

d) Once an object has become eligible for garbage collection, it will remain
eligible until it is destroyed.

e) An object can be finalized only once.


Answer c cannot be correct as the question does mention any active references which refer to obj1 and obj2. If there are any live references which refer to either obj1 or obj2, then both of them will not eligible for garbage collection.

Answer e is correct because finalize() method which every object inherits from java.lang.Object can only be executed only once.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
c) If object obj1 is accessible from obj2 and obj2 is accessible from obj1,
then obj1 and obj2 are not eligible for garbage collection.

If both obj1 & Obj2 form a island of references, then it's eligible for GC.
i.e If instance variables refer to each other, but their links to the outside is null, then these objects are eligible for garbage collection.

e) An object can be finalized only once.
It's True.

Even D is true . Am i right ?
[ July 12, 2005: Message edited by: Srinivasa Raghavan ]
 
shyam ramineni
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Option D is not correct because, a Object which is eligible for Garbage collection can always be resurrected by the finalizer.

In the finalizer block if we assign thisreference to some static variable, then we retrieve the object latter. This will prevent it from garbage collection. But, the next time when the object is eligible for garbage collection then the finalizer block will not be executed.

You can refer Garbage Collection chapter from Khalid Mughal book for reference.
 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Shyam
 
Neelima Chandran
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a bunch for all your valuable points.

Regards,
Neelima
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic