aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Mock Test Question 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 "Garbage Collection Mock Test Question" Watch "Garbage Collection Mock Test Question" New topic
Author

Garbage Collection Mock Test Question

Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
LINK: http://www.javaranch.com/maha/_Mock_Exams/MindQ_s_Sun_Certified_Java_Programmer_Practice_Test.htm

====
37. 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 answer given was a,b and c. However I find that 'a' is the only answer.

Reasons:

'b' is false because, if finalize is called once and the object becomes
ineligible for GC at this time then, when the object is actually garbage collected the next time, finalize is not called. choice 'b' uses "ALWAYS" as a qualifier in its answer and becomes false thus.

'c' is false, because it is legal even if we dont invoke the super class' finalize method in sub class' finalize().

Am I right? Please clarify me on the same.

Btw. Is it always good to call super.finalize() in sub class' finalize method??? Please pass your comments on this. Thanks!


Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
a. is false - Reason, you can not invoke GC. You can only suggest .
b. is correct
c. is false, as any super class can be called independently. So, if superclass is running and subclass calls 'finalize' method of superclass - you can imagine what would happen
d. false. It is unpredictable

What i can see is only b is correct. Please correct if I am wrong.


"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
As per Choice 'a' , What "INVOKE" means is Runtime.getRuntime().gc(), where we invoke Garbage Collector. Whether our call is serviced or not, is left to JVM. So Choice 'a' is right!

See my previous post on the same thread to know, why I feel, Choice 'b' is false.

Others, can you please convince me and Nitin on the answers we have given? Thanks!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
a. Manikandan is correct. The intention of this question is only to test that you know about the System.gc() and Runtime.getRuntime.gc() methods, not whether the garbage collection will actually take place. The wording of the question is rather shaky.

b. correct, if the object is swept up by the garbage collector then the finalize method will be called. Whether or not the finalize method "resurrects" the object is irrelevant, the finalize method will be called by the garbage collector.

c. correct, if you write a finalize method you should call super.finalize() within your method. This is to ensure that the complete object is finalized, not just part of it (remember overriding ?).

d. false, as we all know by now, garbage collection is supposed to be run in a low priority daemon thread snd you cannot predict if, or when, such a thread will run. Also garbage collection algorithms vary from one Java version to another (on some I believe you can select the algorithm to be used on the call to the java runtime when you start the program).

That's my take


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
Barry ! From exam point of you, what does "should" and "must" mean ???

Should do - It is a good practice and appropriate to "do"
Must do - It is compulsory !

Am I right?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection Mock Test Question