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.


What happens to object storage when an object is no longer referenced and becomes garbage collected?

  1. Memory is immediately freed.

  2. Storage is marked and compacted at next garbage collection.

  3. finalize() is called.

  4. Data is overwritten for security.

The correct answer is: Storage is marked and compacted at next garbage collection.

When an object is no longer referenced and becomes garbage collected, the storage for the object is marked and will be compacted during the next garbage collection. This means that the storage space previously occupied by the object will be reused for future objects, resulting in more efficient use of memory. Option A is incorrect because memory is not immediately freed, as garbage collection occurs at a later time. Option C is incorrect because finalize() is called before garbage collection, but it does not necessarily result in immediate memory release. Option D is incorrect because data is only overwritten for security purposes when the storage is reused, not when an object becomes garbage collected.