logo

AQA A-Level Computer Science practice

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.

Try the question first, then check the answer and practise the same topic again.

At a glance

Question

Type

practice

Style

Topic

Abstraction and automation

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.

Related flashcards

No flashcards are published for this page yet.

Related practice questions