Java is the future !: Quiz of the Day | Oct 29, 2010

Friday, October 29, 2010

Quiz of the Day | Oct 29, 2010

Which of the following statements are true?

a. As soon as the reference count for an object reaches zero, it will be immediately garbage collected.
b. The finalize() method will not be invoked more than once by the JVM on the same object.
c. The finalize() method cannot be overloaded.
d. The garbage collection implementation is JVM-dependent.
e. If the finalize() method is overridden, a call to the super class's finalize() method is inserted automatically by the compiler.


Solution : Choices B and D are the correct answers.

As specified by the Java API documentation, the finalize() method is never invoked more than once by a Java virtual machine for any given object. Also, the garbage collection indeed is implementation-dependent. This is what the Java Virtual Machine Specification (JVMS) has to say about memory management -

"The heap is created on virtual machine start-up. Heap storage for objects is reclaimed by an automatic storage management system (known as a garbage collector); objects are never explicitly deallocated. The Java virtual machine assumes no particular type of automatic storage management system, and the storage management technique may be chosen according to the implementor's system requirements."

Choice A is incorrect because when the reference count for an object reaches zero, it becomes eligible for garbage collection but that *does not* necessarily mean that it will be immediately garbage collected.

It is perfectly legal to overload the finalize() method like any other method. However, the JVM will always invoke the default, no arguments finalize() method. Hence choice C is incorrect.

Choice E is incorrect because if you override the finalize() method, you must explicitly call the superclass' finalize() method. The compiler *does not* automatically insert this call. Want to know more?

You can read more about garbage collection at -http://java.sun.com/docs/books/performance/1st_edition/html/JPAppGC.fm.html
Also, the API documentation of the Object class contains the necessary information about the finalize() method -
http://java.sun.com/j2se/1.4/docs/api/java/lang/Object.html


Source : "Facebook community"

No comments:

Post a Comment