Bird
0
0

Which of the following is the correct syntax to convert an int variable num to an Integer object?

easy📝 Syntax Q12 of 15
Java - Wrapper Classes
Which of the following is the correct syntax to convert an int variable num to an Integer object?
A<code>Integer numObj = Integer.valueOf(num);</code>
B<code>Integer numObj = Integer.parseInt(num);</code>
C<code>Integer numObj = num.intValue();</code>
D<code>Integer numObj = Integer.toString(num);</code>
Step-by-Step Solution
Solution:
  1. Step 1: Identify method to get Integer object

    valueOf converts primitive int to Integer object.
  2. Step 2: Check other options

    parseInt returns primitive int, not Integer object. intValue() is a method of Integer, not int. toString returns String, not Integer.
  3. Final Answer:

    Integer.valueOf(num) -> Option A
  4. Quick Check:

    valueOf returns Integer object [OK]
Quick Trick: Use valueOf to get Integer object from int [OK]
Common Mistakes:
  • Using parseInt with int input
  • Calling intValue() on primitive int
  • Confusing toString with valueOf

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes