Oracle
1Z0-803 · Question #30
Given the code fragment: Boolean b1 = true; Boolean b2 = false; int 1 = 0; while (foo) {} Which one is valid as a replacement for foo?
The correct answer is D. "foo".equals("bar"). equals works fine on strings. equals produces a Boolean value. Incorrect answers: the compareTo method produces and int, not a boolean. i = 1 is an assignment, not a comparison. i == 2? -1:0 would produce the integer 0. A Boolean value is needed.
Using Loop Constructs
Question
Given the code fragment:
Boolean b1 = true; Boolean b2 = false; int 1 = 0; while (foo) {} Which one is valid as a replacement for foo?
Options
- Ab1.compareTo(b2)
- Bi = 1
- Ci == 2? -1:0
- D"foo".equals("bar")
How the community answered
(22 responses)- A5% (1)
- B5% (1)
- D91% (20)
Explanation
equals works fine on strings. equals produces a Boolean value. Incorrect answers: the compareTo method produces and int, not a boolean. i = 1 is an assignment, not a comparison. i == 2? -1:0 would produce the integer 0. A Boolean value is needed.
Topics
#while loop#boolean expressions#operators
Community Discussion
No community discussion yet for this question.