Wrapper Classes — Concept Check
Questions on Wrapper Classes. Use the eight wrapper classes to treat primitives as objects, leverage autoboxing, and unlock the rich utility methods on Integer, Double, and friends.
Questions in this quiz
8 questions on Wrapper Classes — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.You have an `Integer` object with a value of 150. What will `Integer.valueOf(150) == Integer.valueOf(150)` return?
- Afalse, because == always returns false for Integer objects
- Btrue, because all Integer objects are cached
- Ctrue, because 150 is automatically cached
- Dfalse, because 150 is outside the Integer cache range
2.A method returns a `Double` object that might be null. What happens if you unbox it directly to a `double`?
- AThe program crashes without an exception
- BA NullPointerException is thrown
- CThe value is converted to NaN
- DThe value is converted to 0.0
3.Given `Integer x = 100; Integer y = 100;`, what will `x == y` evaluate to and why?
- Atrue, because == compares values, not references
- Bfalse, because each Integer is a separate object
- Cfalse, because 100 is not cached
- Dtrue, because 100 is within the Integer cache range
4.You parse a user input string with `Integer.parseInt()`, and it throws an exception. What is the likely cause?
- AThe input string contains a decimal point
- BThe input string is too long
- CThe input string is not a valid integer
- DThe input string is null
5.What is the result of `Integer.parseInt(" 42 ")`?
- ANumberFormatException, due to leading/trailing spaces
- B0, because parseInt ignores non-numeric characters
- C42, because parseInt ignores spaces
- D42, because parseInt trims whitespace
6.A developer uses `Integer.valueOf(300)` repeatedly in a loop. What is the performance implication?
- ANew Integer objects are created each time, impacting performance
- BThe objects are reused, improving performance
- CThe Integer cache optimizes it, so performance is unaffected
- DIt throws an exception due to exceeding cache limits
7.What happens when you compare two `Integer` objects with `equals()`?
- AIt always returns false for values outside the cache
- BIt compares the memory addresses of the Integers
- CIt compares the numerical values of the Integers
- DIt compares the hash codes of the Integers
8.What does `Integer.bitCount(255)` return?
- A6, because bitCount ignores leading zeros
- B9, because 255 includes a sign bit
- C8, because 255 in binary is 11111111
- D7, because 255 is one less than 256