Bird
0
0

Identify the error in the following Java code related to garbage collection:

medium📝 Debug Q14 of 15
Java - Memory Management Basics
Identify the error in the following Java code related to garbage collection:
public class GCExample {
  public static void main(String[] args) {
    Object obj = new Object();
    obj = null;
    System.gc;
  }
}
ACannot assign null to an Object
BObject cannot be garbage collected
CMissing parentheses in System.gc call
DSystem.gc is not a valid method
Step-by-Step Solution
Solution:
  1. Step 1: Locate the syntax error in the code

    System.gc is a method call requiring parentheses (System.gc()); the code has System.gc without them, causing a compilation error. Assigning null to Object and the method's existence are valid.
  2. Final Answer:

    Missing parentheses in System.gc call -> Option C
  3. Quick Check:

    Method calls need parentheses [OK]
Quick Trick: Always use parentheses when calling methods like System.gc() [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Thinking null assignment is invalid
  • Believing System.gc is not a method

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes