Bird
0
0

Given the following code, what will be the output?

hard📝 Application Q15 of 15
Java - Wrapper Classes

Given the following code, what will be the output?

int[] nums = {1, 2, 3};
Integer[] boxedNums = new Integer[nums.length];
for (int i = 0; i < nums.length; i++) {
    boxedNums[i] = nums[i];
}
System.out.println(java.util.Arrays.toString(boxedNums));
ACompilation error due to array assignment
B[null, null, null]
C[1, 2, 3]
D[1, 2, 3, null]
Step-by-Step Solution
Solution:
  1. Step 1: Understand array and autoboxing in loop

    nums is int array. boxedNums is Integer array. Loop assigns each int to Integer element, autoboxing each value.
  2. Step 2: Print the Integer array

    Arrays.toString prints the Integer array elements as their values: [1, 2, 3]. No nulls because all elements assigned.
  3. Final Answer:

    [1, 2, 3] -> Option C
  4. Quick Check:

    Autoboxing fills Integer array from int array correctly [OK]
Quick Trick: Autoboxing works element-wise when assigning primitives to wrapper arrays [OK]
Common Mistakes:
  • Expecting nulls because of object array
  • Thinking array assignment causes error
  • Confusing array length or extra elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes