aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calling Bert Bates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calling Bert Bates" Watch "Calling Bert Bates" New topic
Author

Calling Bert Bates

victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247
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

Joined: Jan 10, 2007
Posts: 247
The question is taken from MindQ's Sun Certified Java Programmer Practice Test Updated 4/7/99.
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
victor,
b) is much debated here in forum.I am confused too.
c) is the answer as it says SHOULD not MUST.


~Sanjeev Singh<br />SCJP 1.5
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
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.


SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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

How?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
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

Joined: Nov 27, 2006
Posts: 10
Also finalize will never run again so if the object is gc'd again finalize will not run.
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
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

Joined: Oct 14, 2002
Posts: 8883
    
    5
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


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

hmmm... how about this?

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


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
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

Joined: Oct 30, 2006
Posts: 14
How about:

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

Dave
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8883
    
    5
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

Joined: Aug 02, 2006
Posts: 152
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 .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling Bert Bates