Bird
0
0

Which of the following is the correct syntax to autobox an int value 5 into an Integer object?

easy📝 Syntax Q3 of 15
Java - Wrapper Classes
Which of the following is the correct syntax to autobox an int value 5 into an Integer object?
AInteger num = 5;
BAll of the above
CInteger num = Integer.valueOf(5);
DInteger num = new Integer(5);
Step-by-Step Solution
Solution:
  1. Step 1: Review each syntax

    Integer num = 5; uses autoboxing (compiler inserts valueOf). A uses explicit constructor (deprecated), C uses explicit factory method.
  2. Step 2: Confirm correctness

    Only D is the syntax for autoboxing, as it triggers automatic conversion.
  3. Final Answer:

    Integer num = 5; -> Option A
  4. Quick Check:

    Autoboxing syntax: direct primitive assignment to wrapper [OK]
Quick Trick: Autoboxing syntax is direct assignment: Integer num = 5; [OK]
Common Mistakes:
  • Confusing explicit constructor or valueOf with autoboxing
  • Thinking all ways to create wrappers use autoboxing
  • Ignoring that autoboxing is compiler-inserted conversion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes