1Z0-808 · Question #29
Given: ``java public class App { public static void main(String[] args) { Boolean[] bool = new Boolean[2]; bool[0] = new Boolean(Boolean.parseBoolean("true")); bool[1] = new Boolean(null)…
The correct answer is A. True false. Boolean.parseBoolean("true") returns the primitive true, so bool[0] wraps to true. Crucially, new Boolean(null) calls the Boolean(String) constructor, which internally treats any non-"true" string-including null-as false, so bool[1] wraps to false, printing true false (option…
Question
public class App {
public static void main(String[] args) {
Boolean[] bool = new Boolean[2];
bool[0] = new Boolean(Boolean.parseBoolean("true"));
bool[1] = new Boolean(null);
System.out.println(bool[0] + " " + bool[1]);
}
}
What is the result?Options
- ATrue false
- BTrue null
- CCompilation fails
- DA NullPointerException is thrown at runtime
How the community answered
(35 responses)- A74% (26)
- B3% (1)
- C9% (3)
- D14% (5)
Explanation
Boolean.parseBoolean("true") returns the primitive true, so bool[0] wraps to true. Crucially, new Boolean(null) calls the Boolean(String) constructor, which internally treats any non-"true" string-including null-as false, so bool[1] wraps to false, printing true false (option A's capital "T" is just answer-choice formatting).
B is wrong because bool[1] holds a Boolean object wrapping false, not a null reference-the array was declared Boolean[] but the slot was assigned a real object.
C is wrong because the code compiles; new Boolean(String) is deprecated in modern Java but not a compile error.
D is wrong because no NPE occurs-new Boolean(null) is perfectly valid and simply evaluates to false rather than throwing.
Memory tip: Think of Boolean.parseBoolean() as "anything that isn't the word true (case-insensitive) becomes false"-nulls, empty strings, and garbage all silently return false, never an exception.
Topics
Community Discussion
No community discussion yet for this question.