Bird
0
0

Given this code, what will be the output?

hard📝 Application Q15 of 15
Java - Wrapper Classes
Given this code, what will be the output?
List list = List.of(1, 2, null, 4);
int sum = 0;
for (Integer n : list) {
    sum += n; // unboxing happens here
}
System.out.println(sum);
ARuntime NullPointerException at unboxing null
BCompilation error due to null in list
CSum ignoring null, prints 7
D10
Step-by-Step Solution
Solution:
  1. Step 1: Understand unboxing in loop with null element

    List contains a null Integer; unboxing null causes error.
  2. Step 2: Identify runtime behavior

    When loop reaches null, unboxing triggers NullPointerException.
  3. Final Answer:

    Runtime NullPointerException at unboxing null -> Option A
  4. Quick Check:

    Unboxing null in loop causes runtime exception [OK]
Quick Trick: Unboxing null in collections causes runtime exception [OK]
Common Mistakes:
  • Assuming null is skipped or treated as zero
  • Expecting compilation error due to null in list
  • Thinking sum is partial ignoring null

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes