Interview Questions: Control Flow
1 Why does Python use indentation instead of curly braces?
Python treats indentation as actual syntax, not just style. This forces code layout to always match code structure, since inconsistent indentation is a real error rather than just an unusual formatting choice a reader has to untangle.
Red flagsSaying indentation in Python is "just for readability" — it is functionally required, not optional style.
Follow-up"What happens if you mix tabs and spaces in the same block?" (Answer: Python raises a TabError or IndentationError.)
2 Why does the order of conditions matter in an elif chain?
Python checks conditions top to bottom and stops at the first one that's true, even if a later condition would also be true. In a grading scale, checking the highest threshold first (>= 75 before >= 60) is essential — reversing the order would make a 90 incorrectly match a lower grade's condition first.
Assuming Python evaluates every condition and picks the "most correct" one — it doesn't; it's strictly first-match-wins.
Follow-up"Rewrite a broken elif chain that checks the lowest grade threshold first." (Good candidates should be able to identify and fix the ordering bug on the spot.)
3 What's the difference between a nested if and using and?
For a simple case like "both conditions must be true," they're equivalent: if a and b: behaves the same as a nested if a: if b:. Nesting becomes necessary once the inner block needs its own independent else, or depends on a value only available inside the outer block.
Nesting ifs by default for every multi-condition check, producing unnecessarily deep, hard-to-read code when a single combined condition would do.
Follow-up"Give an example where nesting is actually required, not just a style choice." (Good answer: when the inner if-else needs to run different logic than the outer's else, producing four distinct outcomes instead of two.)
4 Can an if-else run both its branches? Neither?
No, to both. Exactly one branch always runs in an if-else statement — the condition is either true or false, with no third state, so exactly one path executes every time the statement is reached.
Red flagsConfusing this with multiple separate if statements (not elif), where more than one block genuinely can run since each is independently evaluated.
"What's the key difference between two separate `if` statements and one `if`/`elif`?" (Answer: separate ifs are independent and can both run; an elif chain guarantees only one branch runs.)
Rapid-Fire Review
Quick recall check — fill in each blank and hit Check.