A function assigns letter grades using conditions in this order: if score >= 60 → 'D', elif score >= 70 → 'C', elif score >= 80 → 'B', elif score >= 90 → 'A'. What grade does a score of 95 receive?
A'A' — 95 satisfies the >= 90 condition
B'D' — 95 satisfies >= 60, which is the first condition checked, so the chain stops there
C'C' — elif conditions are checked in ascending order of threshold value
DNo grade — elif chains only run when all prior conditions are false
In an elif chain, the *first* true condition wins and all subsequent branches are skipped. Since 95 >= 60 is true, the function assigns 'D' and never evaluates the remaining conditions. This is why the most restrictive conditions must come first: >= 90, then >= 80, then >= 70, then >= 60 — putting the most permissive condition first intercepts all higher values before they can reach the correct branch.
Question 2 Multiple Choice
A developer needs to print exactly one role-specific message for users who may be 'admin', 'editor', or 'viewer'. Which structure guarantees exactly one branch executes?
AThree separate if statements, one for each role
BAn if / elif / elif chain with one condition per role
CThree separate if statements each followed by a return statement
DA single if statement containing all three role checks nested inside it
An if/elif chain guarantees mutual exclusivity — once a true condition is found, all remaining branches are skipped. Three independent if statements evaluate each condition separately; if the same input satisfied multiple conditions (e.g., through a bug), multiple blocks would execute. The elif structure enforces 'exactly one branch' by design, which is the correct tool whenever categories are mutually exclusive.
Question 3 True / False
In a Python if/elif/elif chain, if the first condition evaluates to True, the interpreter still evaluates the remaining elif conditions to determine if any others also apply.
TTrue
FFalse
Answer: False
The 'first match wins' rule: once a true condition is found in an if/elif chain, its block executes and the entire chain exits — subsequent conditions are never evaluated. This is a fundamental difference from writing separate if statements, where each condition is checked independently. If you need all matching conditions to be evaluated, you must use separate if statements; if you need exactly one branch to run, you use elif.
Question 4 True / False
Replacing an if/else structure with two separate if statements can change a program's behavior when both conditions could be true at the same time.
TTrue
FFalse
Answer: True
In an if/else, exactly one branch always executes — the else clause is a guaranteed alternative. Two separate if statements each check their condition independently, so both blocks can execute when both conditions are true. For example, `if x > 3: print('big')` followed by `if x > 5: print('very big')` both execute when x = 10, whereas `if x > 3: ... else: ...` runs exactly one block. The structures are not equivalent when conditions can overlap.
Question 5 Short Answer
What is the 'first match wins' rule in an elif chain, and why does the order of conditions matter for correctness?
Think about your answer, then reveal below.
Model answer: In an if/elif chain, Python evaluates conditions from top to bottom and executes the block of the first condition that is true, then skips all remaining branches. Because evaluation stops at the first match, broader or less restrictive conditions placed early will intercept cases that were intended for more specific conditions lower in the chain. Correct ordering requires placing the most restrictive conditions first.
A classic example: grading ranges must go from highest to lowest (>= 90, then >= 80, etc.). If >= 60 appears first, every score above 60 gets 'D' because that condition is always satisfied before the others are checked. The elif structure is powerful precisely because only one branch runs — but that power requires the programmer to order conditions so that the right condition matches first for every possible input value.