Mastering Java: The Ultimate Quiz for 'Thinking in Java

Question: 1 / 510

Which statement is true about garbage collection?

Objects are always garbage collected immediately when they become unreachable.

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

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

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.

Get further explanation with Examzify DeepDiveBeta
Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy