Bird
0
0

What will be the output of the following Java code?

medium📝 Predict Output Q13 of 15
Java - Wrapper Classes

What will be the output of the following Java code?

int x = 10;
Integer y = x;
System.out.println(y + 5);
A15
B105
CCompilation error
DRuntime error
Step-by-Step Solution
Solution:
  1. Step 1: Understand autoboxing and unboxing in the code

    Variable x is primitive int 10. Assigning to Integer y autoboxes int to Integer object. In print, y is unboxed to int for addition.
  2. Step 2: Calculate the expression

    y + 5 becomes 10 + 5 = 15, so output is 15.
  3. Final Answer:

    15 -> Option A
  4. Quick Check:

    Autoboxing and unboxing allow arithmetic: 10 + 5 = 15 [OK]
Quick Trick: Autoboxing/unboxing lets Integer behave like int in math [OK]
Common Mistakes:
  • Thinking y + 5 concatenates as string
  • Expecting compilation error due to object usage
  • Confusing autoboxing with no conversion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes