Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Prepare for your Java exam with our ultimate quiz based on 'Thinking in Java'. Engage with expertly crafted questions that enhance your learning process. Perfect for Java enthusiasts looking to solidify their knowledge!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


Can you rely on finalize() method to be called as soon as an object is no longer referenced?

  1. Yes, it's guaranteed.

  2. No, it's not guaranteed.

  3. Only in Java 8 and earlier.

  4. Only if System.gc() is called.

The correct answer is: No, it's not guaranteed.

The finalize() method is used to perform any necessary cleanup actions for an object before it is destroyed by the garbage collector. While it was previously thought to be guaranteed to be called, this is no longer the case due to changes in how Java handles objects and memory management. In some cases, objects may be destroyed even if their finalize() method has not been called. Therefore, it is not safe to rely on this method to be called as soon as an object is no longer referenced. This makes option B the best choice. Options A, C, and D are incorrect because, as mentioned, the finalize() method is not guaranteed to be called and can no longer be relied upon in this way.