Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q13 of 15
Java - Wrapper Classes
What will be the output of the following code?
List list = new ArrayList<>();
list.add(5);
list.add(10);
System.out.println(list.get(0) + list.get(1));
A510
B15
CCompilation error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand autoboxing in collections

    List stores Integer objects. Adding int values 5 and 10 autoboxes them to Integer objects.
  2. Step 2: Evaluate the addition operation

    list.get(0) and list.get(1) return Integer objects, which unbox to int when added, so 5 + 10 = 15.
  3. Final Answer:

    15 -> Option B
  4. Quick Check:

    Autoboxing/unboxing sums to 15 [OK]
Quick Trick: Autoboxing lets you add Integer values like ints [OK]
Common Mistakes:
  • Thinking list.get returns int directly
  • Expecting string concatenation instead of addition
  • Assuming compilation or runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes