aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC question from Khalid Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC question from Khalid" Watch "GC question from Khalid" New topic
Author

GC question from Khalid

bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
This one took me a while to figure out, even though there is no code.
Which statements describe guaranteed behavior of the garbage collection and finalization mechanisms?
A. Objects are deleted when they can no longer be accessed through any reference
B. The finanalize() method will eventually be called on every object
C. The finanalize() method will never be called more than once on an object
D. An object will not get garbage collected as long as it is possible for an active part of the program to access it through a reference.
E. The garbage collector will use a mark and sweep algorithm.

Bill
Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
A. Objects are deleted when they can no longer be accessed through any reference
- True.
B. The finalize() method will eventually be called on every object
- True
C. The finalize() method will never be called more than once on an object.
- True
D. An object will not get garbage collected as long as it is possible for an active part of the program to access it through a
reference.
- True
E. The garbage collector will use a mark and sweep algorithm.
- true. But not so sure about it.
Tejovikas Suravajjala
Greenhorn

Joined: Dec 21, 2000
Posts: 4
*Java invokes finanalize() for all objects when the program exits(assuming it does not happen earlier).
*This happens just before the object is about to be Garbage collected.
Bill asks for "guaranteed behavior" and not what is true so....
A: Guaranteed behavior, eventually.
B: Guaranteed
C: I would not bet on this
D: guaranteed
E:I do not have a clue on this. I need to sweep some books myself.
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Aru, not correct. I'll make it easier and make it like the new exam format. Select the two correct answers.

Bill
Sivaram Ghorakavi
Ranch Hand

Joined: Nov 30, 2000
Posts: 56
A,B,C,D true. Not sure about E
Sivaram Ghorakavi
Ranch Hand

Joined: Nov 30, 2000
Posts: 56
Two, I think would be C & D
Aru Ven
Ranch Hand

Joined: Sep 28, 2000
Posts: 199
C & D are guaranteed.
Aruna
[This message has been edited by Aru Ven (edited December 21, 2000).]
rajani peddi
Ranch Hand

Joined: Nov 27, 2000
Posts: 73
Bill,
I think that C and D are the correct answers.
rajani
Balamurugan Kandasamy
Greenhorn

Joined: Dec 03, 2000
Posts: 19
A. Objects are deleted when they can no longer be accessed through any reference
False

Reason : They are eligible for deletion would have been true.
B. The finanalize() method will eventually be called on every object
True

Reason : The objects finalize method would be implicitly called
(if not overidden) just before GC and for that matter all objects are eventually GCed at one point.
C. The finalize() method will never be called more than once on an object
True

Reason : As said above finalize would be called just before GCing the object. And an object would be GC'ed only once. And also note that even if there is an interruption during finalize() method execution , it wont be called again.
D. An object will not get garbage collected as long as it is possible for an active part of the program to access it through a reference.
True

Reason : Y disturb something that is useful :-). Well the compiler feels so !!!
E. The garbage collector will use a mark and sweep algorithm.

false

Reason : Not necessarily. Depends on JVM . This is again system dependent similar to time-slicing/ pre-emptive scheduling of threads.
Bill pls let know if something looks odd.
MKBala...
Tejovikas Suravajjala
Greenhorn

Joined: Dec 21, 2000
Posts: 4
Guys,
The book that I hold in my hand explicitly says:
"The official documentation says that finalize() will be run for all objects when the program exits."
--Java 2 Exam Guide. Barry Boone/William Stanek
That makes
B: True
Balamurugan Kandasamy
Greenhorn

Joined: Dec 03, 2000
Posts: 19
Oops!!! Sorry about point B.
There is no guarantee that all objects need to be GC'ed.
So I too go with C and D.
MKBala...
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
The correct answers according to Khalid is C and D.
D is the obvious one, and at first I thought B and C were also true. But I tried some code where I overrote finalize, given here:

And I do not get the message in the finalize code, which shows that the finalize method is not always guarnanteed to run. It will always run before the object is GC'd, but the object is not always GC'd.
As for C, I looked that one up in the JLS:

Because of the way that an object progresses from the unfinalized state through the finalizable state to the finalized state, the finalize method is never automatically invoked more than once by a Java virtual machine for each object, even if the object is again made reachable after it has been finalized.

or from the API

The finalize method is never invoked more than once by a Java virtual machine for any given object.

So C is true.
As for A and E. A is false since it will be eligble for GC whenever there are no more references.
And E is incorrect because that is left up to the java implemantion on how they want to process the GC.
Bill
rajani peddi
Ranch Hand

Joined: Nov 27, 2000
Posts: 73

Khalid doesn't give answers to his questions which is very troublesome. I am answering 1 question and ending the exam. But if that goes wrong i have to wait till i get that question again and then try the next possible answer (also memorizing what i have answered earlier).
How are everyone going about this test? I want to copy the applet in a word document. Can anyone help me with this.
Thank u
rajani
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
rajani,
That is exactly what I am doing. I am answering the questions one at a time, but before I answer it, I copy the code and all the answers in a word doc, and then I try my answer. Once I get it, I highlight the answer and explain why it is correct and why everything else is incorrect.
Time consuming, but really helps me learn it. I don't have all of them yet, but as I get them I am posting them here so everyone can comment on it and get the correct answer with explanations. I am doing about 3 a day.
Bill
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
I highlight the question and hit CONTROL C and then CONTROL V in Word, and for the answers, I have to type those in manually.
rajani peddi
Ranch Hand

Joined: Nov 27, 2000
Posts: 73
Thanks Bill
bhakti soman
Greenhorn

Joined: Nov 12, 2000
Posts: 22
hi all
i would like the answer to question B to be more clear
do all objects are GC once ,why and when???
please guide me
anjan bhushan
Ranch Hand

Joined: Dec 12, 2000
Posts: 71
It seems java garbage collects as per memory optimization.when it
feels that the program is going to be killed and there is no more after this.It escapes it :-))


I am the most eligible bachelor in whole world, but only known in limited territory!!!
Digital Intoxication Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: GC question from Khalid