Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to unbox the Integer object to an int primitive.
Java
Integer num = 10; int value = num[1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using toString() returns a String, not an int.
Using valueOf() is a static method to box, not unbox.
✗ Incorrect
The method intValue() converts the Integer object to a primitive int.
2fill in blank
mediumComplete the code to assign the unboxed Double value to a double variable.
Java
Double d = 5.5; double val = d[1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using intValue() loses decimal precision.
Using toString() returns a String, not a double.
✗ Incorrect
doubleValue() returns the primitive double from a Double object.
3fill in blank
hardFix the error in unboxing the Boolean object to a boolean primitive.
Java
Boolean flag = Boolean.TRUE;
boolean isTrue = flag[1];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.
✗ Incorrect
booleanValue() returns the primitive boolean from a Boolean object.
4fill in blank
hardFill 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"); }
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.
✗ Incorrect
charValue() unboxes Character to char, which can be compared to a char literal 'a'.
5fill in blank
hardFill 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); }
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.
✗ Incorrect
intValue() and doubleValue() unbox the wrapper objects; + adds the values in the output.
