Java programming — Boolean operators with single ampersand/pipe (non–short-circuit) and overall truth evaluation: class SSBool { public static void main(String [] args) { boolean b1 = true; boolean b2 = false; boolean b3 = true; if ( b1 & b2 | b2 & b3 | b2 ) System.out.print("ok "); if ( b1 & b2 | b2 & b3 | b2 | b1 ) System.out.println("dokey"); } }

Difficulty: Easy

Correct Answer: dokey

Explanation:


Introduction / Context:
This question focuses on non–short-circuit boolean operators & and | and how they combine in expressions to produce a final truth value.



Given Data / Assumptions:

  • b1 = true, b2 = false, b3 = true.
  • Operators used: & and | on booleans (both evaluate both operands).


Concept / Approach:
Compute each term and then OR the results. Remember & yields true only if both sides are true, and | yields true if either side is true.



Step-by-Step Solution:

First if: b1 & b2 = true & false = false; b2 & b3 = false & true = false; expression becomes false | false | false = false; nothing printed.Second if: same first parts are false, but there is an extra | b1 at the end; false | false | false | true = true, so prints "dokey".


Verification / Alternative check:
Replace with parentheses to verify grouping, or temporarily convert to booleans to visualize each term.



Why Other Options Are Wrong:
They either print "ok" incorrectly or claim no output/compile error, both of which contradict the truth table.



Common Pitfalls:
Confusing & and | with their short-circuit counterparts && and ||; mixing precedence without parentheses (though all terms here are associative with |).



Final Answer:
dokey

More Questions from Operators and Assignments

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion