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.


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.