• 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

Calling Bert Bates

 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is taken from a Mock exam; (originally posted by Rita Kapoor.)


Which of the following statements about Java's garbage collection are true?

a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.

The given answer is a, b, c.

I agree with a. But b & c are not correct; see K&B pg 253.

Bert we need your help.
 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is taken from MindQ's Sun Certified Java Programmer Practice Test Updated 4/7/99.
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
victor,
b) is much debated here in forum.I am confused too.
c) is the answer as it says SHOULD not MUST.
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
b) The finalize method is always called before an object is garbage collected.

Isn't there some ambiguity in that statement?.

Finalize occurs 1 time before and object is garbage collected (when called by the jvm), but it does not happen just the moment before. The object can be resurrected or saved and then Garbage Collected.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Javier Sanchez Cerrillo:
...The object can be resurrected or saved and then Garbage Collected.


How?
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inside the finalize method you can pass a "this" reference to another object and then you prevent that object from being Garbage Collected.

Although you are not really resurrecting an object, that's the term Sun uses.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also finalize will never run again so if the object is gc'd again finalize will not run.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the finalize is still called before the object is finalized (at some point before). I'd call this a matter of symantics.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Sorry, I've been offline for a few days.

IMHO perhaps the hardest SCJP topic to write mock questions for is GC. If the language is even just slightly vague, the whole question can be ruined. I have to say that the authors did a pretty good job with this question, but not quite good enough - I think that the way B, C and D were all worded leaves a little room for misinterpretation. I do agree with the discussion concerning resurrection however, and you might encounter a question on the real exam that uses this concept.

Given the difficulty of getting good quality GC questions, i think this is an excellent place to post them and discuss them. I will offer up an idea:

Why don't you guys take a stab at improving the language of the answers for this question? It would be a great exercise for everyone involved!

hth,

Bert
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmmm... how about this?

b) The finalize method is always called exactly once sometime before an object is garbage collected.
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hmmm... how about this?

b) The finalize method is always called exactly once sometime before an object is garbage collected.



I think that is still ambiguous. You can call manually the finalize method 1000 times and it will be called 1 more time by the garbage collector before it is garbage collected.

Maybe:

The finalize method is always called exactly one by the garbage collector before (not "just before") and object is garbage collected.
[ January 26, 2007: Message edited by: Javier Sanchez Cerrillo ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about:

The garbage collector will invoke the finalize method of an object exactly once.

Dave
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A-ha!

So you see that writing these mock questions about GC isn't easy? I'd say there are still tiny flaws in all of these ideas...

 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This forum is great, you Bert are too, If your book is the Bible for the Java Learners you must be Mohamed of something like that .
 
reply
    Bookmark Topic Watch Topic
  • New Topic