Java is the future !: Quiz of the Day | November 3, 2010

Wednesday, November 3, 2010

Quiz of the Day | November 3, 2010

What will be the result of running the following code? 
a.) The output: Value is - Hello
b.) The output: Value is - Hello world
c.) A compiler error
d.) No output
e.) An exception

Solution : Choice B is the correct answer.

Since the OR operator used here is not the short-circuit operator (which is "||"), both operands are evaluated even though the first operanditself is true which guarantees that the whole condition is true. Thus the value of the StringBuffer instance s after the evaluation of the boolean condition is "Hello world" and hence choice B is correct.

Please note here that if we had used "||" operator instead of "|", the correct answer would have been choice A, because in that case the right-hand side operand wouldn't have been evaluated as the left-hand side operand would have been true. 

Source : "Facebook community"

No comments:

Post a Comment