Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Java - Wrapper Classes
What will be the output of this code?
List list = new ArrayList<>(); list.add(10); list.add(null); System.out.println(list.get(1));
ACompilation error
Bnull
C0
DRuntime exception
Step-by-Step Solution
Solution:
  1. Step 1: Analyze list contents

    The list contains Integer objects: 10 and null (explicitly added).
  2. Step 2: Printing the second element

    list.get(1) returns null, printing null outputs 'null' string.
  3. Final Answer:

    null -> Option B
  4. Quick Check:

    Null in list prints as 'null' string = C [OK]
Quick Trick: Null wrapper objects print as 'null' when printed [OK]
Common Mistakes:
  • Expecting 0 instead of null
  • Thinking null causes compile or runtime error here
  • Confusing null with default primitive values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes