Exam-style question
Try this first
A programmer writes the following Python function to return True when exactly two of three switches are on.\n\ndef exactly_two(a, b, c):\n return (a and b) or c\n\na) Explain why this solution is incorrect.\nb) Give a corrected Boolean expression for the return statement.\nc) Describe a systematic way to check that the corrected solution works.
Answer and explanation
Concise answer
a) The expression returns True whenever c is True, even if neither a nor b is True or if all three switches are True.
Why this is correct
For exactly two true inputs, the solution must identify each possible pair and explicitly require that the remaining input is false. Three Boolean inputs have 2^3 = 8 combinations, so checking all eight combinations provides a complete check for this problem.
Use the concise answer above for a quick check, then reveal the full model answer and detailed explanation.
Common mistake
No common mistake is linked to this question yet.
