Challenge - 5 Problems
Wrapper Methods Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Integer wrapper method usage
What is the output of the following Java code snippet using Integer wrapper methods?
Java
public class Main { public static void main(String[] args) { Integer num = Integer.valueOf("123"); int result = num.intValue() + Integer.parseInt("77"); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Remember Integer.valueOf returns an Integer object and parseInt returns a primitive int.
✗ Incorrect
Integer.valueOf("123") creates an Integer object with value 123. intValue() extracts the primitive int 123. Integer.parseInt("77") returns int 77. Adding 123 + 77 equals 200.
❓ Predict Output
intermediateOutput of Double wrapper method usage
What will be printed by this Java code using Double wrapper methods?
Java
public class Main { public static void main(String[] args) { Double d1 = Double.valueOf("10.5"); double d2 = Double.parseDouble("2.5"); System.out.println(d1.doubleValue() * d2); } }
Attempts:
2 left
💡 Hint
Check the multiplication of 10.5 and 2.5 as doubles.
✗ Incorrect
Double.valueOf("10.5") returns a Double object with value 10.5. doubleValue() extracts 10.5 as primitive double. Double.parseDouble("2.5") returns primitive double 2.5. Multiplying 10.5 * 2.5 equals 26.25.
❓ Predict Output
advancedOutput of Boolean wrapper method usage
What is the output of this Java code snippet using Boolean wrapper methods?
Java
public class Main { public static void main(String[] args) { Boolean b1 = Boolean.valueOf("true"); Boolean b2 = Boolean.valueOf("False"); System.out.println(b1 && b2); } }
Attempts:
2 left
💡 Hint
Boolean.valueOf is case insensitive and returns Boolean objects. Logical AND on Boolean objects requires unboxing.
✗ Incorrect
Boolean.valueOf("true") returns Boolean.TRUE. Boolean.valueOf("False") returns Boolean.FALSE. Using && on Boolean objects unboxes them to primitive boolean. true && false is false.
❓ Predict Output
advancedOutput of Character wrapper method usage
What will this Java code print when using Character wrapper methods?
Java
public class Main { public static void main(String[] args) { Character ch = Character.valueOf('a'); System.out.println(Character.toUpperCase(ch)); } }
Attempts:
2 left
💡 Hint
Character.toUpperCase converts a character to uppercase.
✗ Incorrect
Character.valueOf('a') returns a Character object wrapping 'a'. Character.toUpperCase converts 'a' to 'A'.
❓ Predict Output
expertOutput of complex wrapper method chaining
What is the output of this Java code using multiple wrapper methods?
Java
public class Main { public static void main(String[] args) { String s = "256"; Integer i = Integer.valueOf(s); Double d = Double.valueOf(i.doubleValue() / 4); System.out.println(d.intValue() + ":" + d); } }
Attempts:
2 left
💡 Hint
Trace the conversions from String to Integer to Double and back to int.
✗ Incorrect
Integer.valueOf("256") is 256. i.doubleValue() / 4 is 256.0 / 4 = 64.0. Double.valueOf(64.0) is Double object 64.0. d.intValue() is 64. Printing d.intValue() + ":" + d prints "64:64.0".
