Bird
0
0

Which of the following code snippets correctly uses autoboxing to assign a primitive int to an Integer object?

easy📝 Syntax Q3 of 15
Java - Wrapper Classes

Which of the following code snippets correctly uses autoboxing to assign a primitive int to an Integer object?

AInteger num = (Integer) 25;
BInteger num = new Integer(25);
CInteger num = 25;
DInteger num = Integer.parseInt(25);
Step-by-Step Solution
Solution:
  1. Step 1: Recognize autoboxing syntax

    Autoboxing allows direct assignment of a primitive to its wrapper class without explicit constructor or casting.
  2. Step 2: Evaluate options

    Integer num = 25; uses direct assignment which triggers autoboxing. Integer num = new Integer(25); uses explicit constructor (not autoboxing). Integer num = (Integer) 25; is invalid casting. Integer num = Integer.parseInt(25); is invalid usage of parseInt.
  3. Final Answer:

    Integer num = 25; -> Option C
  4. Quick Check:

    Direct assignment of int to Integer triggers autoboxing [OK]
Quick Trick: Assign primitive directly to wrapper for autoboxing [OK]
Common Mistakes:
  • Using explicit constructor instead of autoboxing
  • Trying to cast primitive to wrapper
  • Misusing parseInt which returns int, not Integer

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes