Bird
0
0

Identify the problem in this Java code related to garbage collection:

medium📝 Debug Q6 of 15
Java - Memory Management Basics
Identify the problem in this Java code related to garbage collection:
public class FinalizeTest {
  public static void main(String[] args) {
    Object obj = new Object();
    obj.finalize();
  }
}
AThe code will cause a compile-time error
BObject cannot be instantiated directly
CCalling finalize() directly is discouraged and may cause unexpected behavior
DGarbage collection is forced by calling finalize()
Step-by-Step Solution
Solution:
  1. Step 1: Understand finalize()

    finalize() is a method called by the garbage collector before reclaiming an object.
  2. Step 2: Analyze direct call

    Calling finalize() directly is discouraged because it does not trigger garbage collection and may cause side effects.
  3. Step 3: Evaluate other options

    Object cannot be instantiated directly is incorrect; Object can be instantiated. The code will cause a compile-time error is wrong; code compiles. Garbage collection is forced by calling finalize() is false; finalize() does not force GC.
  4. Final Answer:

    Calling finalize() directly is discouraged and may cause unexpected behavior -> Option C
  5. Quick Check:

    Do not call finalize() manually [OK]
Quick Trick: Never call finalize() directly; JVM calls it [OK]
Common Mistakes:
  • Thinking finalize() triggers garbage collection
  • Believing direct finalize() call is safe
  • Assuming Object cannot be instantiated

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes