Given the code fragment:
Boolean b1 = true;
Boolean b2 = false;
int 1 = 0;
while (foo) {}
Which one is valid as a replacement for foo?
A.
b1.compareTo(b2)
B.
i = 1
C.
i == 2? -1:0
D.
“foo”.equals(“bar”)
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.
“D”
@Firstly it gives a Compile Time Error, at the line //int 1 = 0;
if it is ‘l’: L not ‘1’: one -> then it doesn’t thrown an Compile Time Error.
if So, then “D” : “foo”.equals(“bar”) returns a Boolean Value.
Which is Valid.