Bird
Raised Fist0
Javaprogramming~10 mins

Unboxing in Java - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to unbox the Integer object to an int primitive.

Java
Integer num = 10;
int value = num[1];
Drag options to blanks, or click blank then click option'
A.toString()
B.valueOf()
C.intValue()
D.doubleValue()
Attempts:
3 left
💡 Hint
Common Mistakes
Using toString() returns a String, not an int.
Using valueOf() is a static method to box, not unbox.
2fill in blank
medium

Complete the code to assign the unboxed Double value to a double variable.

Java
Double d = 5.5;
double val = d[1];
Drag options to blanks, or click blank then click option'
A.toString()
B.intValue()
C.floatValue()
D.doubleValue()
Attempts:
3 left
💡 Hint
Common Mistakes
Using intValue() loses decimal precision.
Using toString() returns a String, not a double.
3fill in blank
hard

Fix the error in unboxing the Boolean object to a boolean primitive.

Java
Boolean flag = Boolean.TRUE;
boolean isTrue = flag[1];
Drag options to blanks, or click blank then click option'
A.booleanValue()
B.intValue()
C.toString()
D.doubleValue()
Attempts:
3 left
💡 Hint
Common Mistakes
Using intValue() causes a compile error because Boolean has no such method.
Using toString() returns a String, not a boolean.
4fill in blank
hard

Fill both blanks to unbox the Character object and compare it to a char literal.

Java
Character ch = 'a';
if (ch[1] == '[2]') {
    System.out.println("Match");
}
Drag options to blanks, or click blank then click option'
A.charValue()
Ba
Cb
D.toString()
Attempts:
3 left
💡 Hint
Common Mistakes
Using toString() returns a String, which cannot be compared to a char literal.
Comparing to the wrong char literal causes the condition to fail.
5fill in blank
hard

Fill all three blanks to create a map with unboxed keys and values from wrapper objects.

Java
Map<Integer, Double> map = new HashMap<>();
Integer key = 1;
Double value = 2.5;
map.put(key[1], value[2]);

for (Map.Entry<Integer, Double> entry : map.entrySet()) {
    int k = entry.getKey();
    double v = entry.getValue();
    System.out.println(k [3] v);
}
Drag options to blanks, or click blank then click option'
A.intValue()
B.doubleValue()
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Not unboxing before putting into the map uses the wrapper objects directly.
Using - instead of + changes the output meaning.
Forgetting to unbox compiles but does not demonstrate explicit unboxing.

Practice

(1/5)
1. What does unboxing mean in Java?
easy
A. Converting a primitive type to a String
B. Automatically converting a wrapper object to its primitive type
C. Manually converting a primitive type to a wrapper object
D. Automatically converting a String to a wrapper object

Solution

  1. Step 1: Understand wrapper and primitive types

    Wrapper classes like Integer wrap primitive types like int.
  2. Step 2: Define unboxing

    Unboxing is the automatic conversion from wrapper object to primitive type.
  3. Final Answer:

    Automatically converting a wrapper object to its primitive type -> Option B
  4. Quick Check:

    Unboxing = wrapper to primitive [OK]
Hint: Unboxing means wrapper to primitive automatically [OK]
Common Mistakes:
  • Confusing unboxing with boxing (primitive to wrapper)
  • Thinking unboxing converts to String
  • Assuming unboxing requires manual code
2. Which of the following Java statements correctly uses unboxing?
easy
A. Integer num = 10; int val = num;
B. Integer val = new Integer(10);
C. Integer num = int 10;
D. Integer val = Integer.valueOf(10);

Solution

  1. Step 1: Check each option for correct syntax and unboxing

    Integer num = 10; int val = num; assigns Integer object to int variable, triggering unboxing automatically.
  2. Step 2: Identify errors in other options

    Integer val = new Integer(10); assigns wrapper object to wrapper variable (no unboxing); Integer num = int 10; has invalid syntax; Integer val = Integer.valueOf(10); assigns wrapper object to wrapper variable (no unboxing).
  3. Final Answer:

    Integer num = 10; int val = num; -> Option A
  4. Quick Check:

    Wrapper to primitive assignment triggers unboxing [OK]
Hint: Assign wrapper object to primitive variable to unbox [OK]
Common Mistakes:
  • Assigning wrapper constructors directly to wrapper variables (no unboxing)
  • Incorrect syntax like 'Integer num = int 10;'
  • Confusing boxing and unboxing syntax
3. What is the output of this Java code?
Integer a = 5;
int b = a + 10;
System.out.println(b);
medium
A. NullPointerException
B. 510
C. Compilation error
D. 15

Solution

  1. Step 1: Understand unboxing in expression

    Variable 'a' is Integer object; adding 10 triggers unboxing 'a' to int 5.
  2. Step 2: Calculate the sum and print

    5 + 10 = 15, so output is 15 printed.
  3. Final Answer:

    15 -> Option D
  4. Quick Check:

    Unboxing Integer 5 + 10 = 15 [OK]
Hint: Unboxing happens automatically in arithmetic expressions [OK]
Common Mistakes:
  • Thinking it concatenates as string '510'
  • Expecting compilation error due to wrapper usage
  • Confusing with NullPointerException if variable was null
4. Identify the error in this code snippet:
Integer num = null;
int val = num;
System.out.println(val);
medium
A. Prints 0 without error
B. Compilation error due to null assignment
C. Runtime NullPointerException during unboxing
D. Prints 'null' as output

Solution

  1. Step 1: Analyze unboxing of null Integer

    Variable 'num' is null; unboxing null to int causes runtime error.
  2. Step 2: Identify exception thrown

    Unboxing null throws NullPointerException at runtime.
  3. Final Answer:

    Runtime NullPointerException during unboxing -> Option C
  4. Quick Check:

    Unboxing null causes NullPointerException [OK]
Hint: Unboxing null causes runtime NullPointerException [OK]
Common Mistakes:
  • Expecting compilation error instead of runtime error
  • Assuming null unboxes to zero
  • Thinking it prints 'null' string
5. Given this code, what will be the output?
List<Integer> list = List.of(1, 2, null, 4);
int sum = 0;
for (Integer n : list) {
    sum += n; // unboxing happens here
}
System.out.println(sum);
hard
A. Runtime NullPointerException at unboxing null
B. Compilation error due to null in list
C. Sum ignoring null, prints 7
D. 10

Solution

  1. Step 1: Understand unboxing in loop with null element

    List contains a null Integer; unboxing null causes error.
  2. Step 2: Identify runtime behavior

    When loop reaches null, unboxing triggers NullPointerException.
  3. Final Answer:

    Runtime NullPointerException at unboxing null -> Option A
  4. Quick Check:

    Unboxing null in loop causes runtime exception [OK]
Hint: Unboxing null in collections causes runtime exception [OK]
Common Mistakes:
  • Assuming null is skipped or treated as zero
  • Expecting compilation error due to null in list
  • Thinking sum is partial ignoring null
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard