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

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

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


Which statement is true about garbage collection?

  1. Objects are always garbage collected immediately when they become unreachable.

  2. Garbage collection can be forced by calling System.gc().

  3. Java guarantees that finalize() will be called on an object

  4. Garbage collection ensures that a program does not run out of memory.

The correct answer is: Garbage collection ensures that a program does not run out of memory.

Garbage collection is an automatic process that runs in the background of a Java program to reclaim memory used by objects that are no longer reachable. This process ensures that a program does not run out of memory by freeing up space for new objects as needed. Option A is incorrect because not all objects are garbage collected immediately, as it depends on the specific garbage collector being used. Option B is incorrect because while it is possible to force garbage collection through System.gc(), there is no guarantee that it will actually run. Option C is incorrect because while Java does have a finalize() method, there is no guarantee that it will be called on an object before it is garbage collected. Therefore, the correct answer is D.