• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Bert Bates

 
victor kamat
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
The question is taken from MindQ's Sun Certified Java Programmer Practice Test Updated 4/7/99.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
  • 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.
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Paul Lachance
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also finalize will never run again so if the object is gc'd again finalize will not run.
 
Andy Morris
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • 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
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
David Grindley
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about:

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

Dave
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • 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
  • 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 .
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic