Thursday 21 February 2013

18. What will happen when you attempt to compile and run the following code?

18.  What will happen when you attempt to compile and run the following code?


Choices:
a. Compilation error, attempting to perform binary comparison on logical data type.
b. Compilation and output of "We are equal 10".
c. Compilation and output of "Not equal! 20".
d. Compilation and output of "Not equal! 10".


Answer:-
D is correct.
The output will be "Not equal! 10".
Please note that && is short-circuit logical AND operator.
If first operand(before &&) is false(or evaluated to false) then the other operand will not be evaluated.
This illustrates that the Output +=10 calculation was never performed because processing stopped after the first operand was evaluated to be false.
If you change the value of b1 to true processing occurs as you would expect and the output would be "We are equal 20".

No comments:

Post a Comment