Consider the following code segment. What is printed as a result of executing the code segment?
boolean p = true;
boolean q = false;
boolean left = !(p && q);
boolean right = !p || !q;
System.out.print(left == right);
truefalsealeft and right hold the same value and true is printed.
Explanation
!(p && q) and !p || !q form a De Morgan pair: the negation moves inward and the operator changes from && to ||. Equivalent expressions agree for every combination of values, so both variables hold true here.
- B is not correct:
falsewould require the two expressions to disagree, but De Morgan's Laws make them equivalent, so they agree for every combination ofpandq. - C is not correct: the letter
anames an operand in the statement of De Morgan's Laws; it is not a value that a comparison of twobooleanvariables can produce. - D is not correct: nothing in the segment breaks a rule: each of the first four lines declares a
booleanvariable, and the last line prints the result of a comparison.