Bird
0
0

What will be the output of the following Java code snippet?

medium📝 Predict Output Q4 of 15
Java - Wrapper Classes

What will be the output of the following Java code snippet?

int num = 15;
Integer boxedNum = num;
System.out.println(boxedNum * 2);
ACompilation error
B30
C15
DNullPointerException
Step-by-Step Solution
Solution:
  1. Step 1: Understand autoboxing and unboxing

    The primitive int 'num' is autoboxed to Integer 'boxedNum'. When used in arithmetic, 'boxedNum' is unboxed back to int.
  2. Step 2: Calculate the expression

    boxedNum * 2 equals 15 * 2 = 30.
  3. Final Answer:

    30 -> Option B
  4. Quick Check:

    Autoboxing and unboxing allow arithmetic without errors [OK]
Quick Trick: Autoboxing and unboxing allow seamless arithmetic [OK]
Common Mistakes:
  • Expecting a compilation error due to mixing types
  • Thinking the output is the original number
  • Assuming NullPointerException without null assignment

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes