Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q14 of 15
Java - Wrapper Classes
Identify the error in this code snippet:
Integer num = null;
int val = num;
System.out.println(val);
APrints 0 without error
BCompilation error due to null assignment
CRuntime NullPointerException during unboxing
DPrints 'null' as output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze unboxing of null Integer

    Variable 'num' is null; unboxing null to int causes runtime error.
  2. Step 2: Identify exception thrown

    Unboxing null throws NullPointerException at runtime.
  3. Final Answer:

    Runtime NullPointerException during unboxing -> Option C
  4. Quick Check:

    Unboxing null causes NullPointerException [OK]
Quick Trick: Unboxing null causes runtime NullPointerException [OK]
Common Mistakes:
  • Expecting compilation error instead of runtime error
  • Assuming null unboxes to zero
  • Thinking it prints 'null' string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes