Python if-else MCQ questions with answers
Which branch runs, and why: if, elif, else, truthiness and match.
0 of 20 answered
Question 1 · Easy
What is the output of this code?
marks = 75 if marks >= 90: print("A") elif marks >= 70: print("B") elif marks >= 50: print("C") else: print("D")
Show the answer
Answer: C
Python checks the conditions from the top and runs only the first branch whose condition is true. 75 fails
Run this code in the editor>= 90but passes>= 70, so it printsBand skips the rest, even though>= 50is also true.Question 2 · Medium
What is the output of this code?
x = 15 if x > 10: print("big") if x > 5: print("medium") else: print("small")
Show the answer
Answer: B
These are two separate
Run this code in the editorifstatements, not anif/elifchain, so both are checked. 15 > 10 printsbig, then 15 > 5 printsmedium; theelsebelongs only to the secondif.Question 3 · Hard
What is the output of this code?
values = [0, "", [], None, "0", [0]] for v in values: if v: print(repr(v))
Show the answer
Answer: A
Run this code in the editor0, the empty string, the empty list andNoneare all falsy. The string"0"is not empty and the list[0]has one item, so both are truthy, whatever they contain.Question 4 · Easy
What is the output of this code?
x = None if x: print("yes") elif x == 0: print("zero") else: print("no")
Show the answer
Answer: D
Run this code in the editorNoneis falsy, so the first branch is skipped.None == 0isFalsebecauseNoneis not a number, so theelsebranch printsno.Question 5 · Easy
What is the output of this code?
age = 17 status = "adult" if age >= 18 else "minor" print(status)
Show the answer
Answer: B
A conditional expression
Run this code in the editorA if condition else BgivesAwhen the condition is true andBotherwise. 17 >= 18 is false, sostatusis"minor".Question 6 · Easy
What is the output of this code?
x = 7 if x > 5: if x % 2 == 0: print("big even") else: print("big odd") else: print("small") print("done")
Show the answer
Answer: C
7 > 5, so the inner
Run this code in the editorifruns; 7 % 2 is 1, so itselseprintsbig odd. The lastprintis not indented, so it runs after the wholeifstatement, whichever branch was taken.Question 7 · Medium
What is the output of this code?
n = 4 if n > 5: print("A") if n > 2: print("B") else: print("C") if n > 2: print("D")
Show the answer
Answer: A
Indentation decides which lines belong to which block.
Run this code in the editorn > 5is false, so the whole indented block, including the nestedif, is skipped andelseprintsC. The lastifis a separate statement, and 4 > 2 printsD.Question 8 · Hard
What is the output of this code?
choice = 5 print(choice == 1 or 2) if choice == 1 or 2: print("one or two") else: print("other")
Show the answer
Answer: B
Run this code in the editorchoice == 1 or 2means(choice == 1) or 2. The comparison isFalse, soorreturns its second operand, 2, which is truthy, and theifalways runs. The correct test ischoice == 1 or choice == 2, orchoice in (1, 2).Question 9 · Medium
What is the output of this code?
command = "stop" match command: case "start": print("Starting") case "stop" | "halt": print("Stopping") case _: print("Unknown")
Show the answer
Answer: C
Run this code in the editormatchruns only the firstcasethat matches; there is no fall-through to later cases."stop" | "halt"matches either value, so it printsStoppingand thecase _default is skipped.Question 10 · Hard
What is the output of this code?
point = (0, 5) match point: case (0, 0): print("origin") case (0, y): print(f"on the y axis at {y}") case (x, 0): print(f"on the x axis at {x}") case _: print("somewhere else")
Show the answer
Answer: D
Run this code in the editor(0, 0)fails because the second item is 5. Incase (0, y):, the 0 must match exactly while the nameycaptures whatever is in that position, soybecomes 5 and that case runs.Question 11 · Medium
What is the output of this code?
temp = 35 if temp > 20: print("warm") elif temp > 30: print("hot") else: print("cold")
Show the answer
Answer: D
35 > 20 is true, so the first branch prints
Run this code in the editorwarmand the chain ends there;temp > 30is never checked. When conditions overlap, put the strictest one (> 30) first.Question 12 · Medium
What is the output of this code?
n = 0 sign = "positive" if n > 0 else "negative" if n < 0 else "zero" print(sign)
Show the answer
Answer: A
Chained conditional expressions are read like
Run this code in the editorif/elif/else:n > 0is false, so Python evaluates"negative" if n < 0 else "zero", and 0 < 0 is also false, givingzero.Question 13 · Easy
Which error does this code raise?
x = 10 if x > 5 print("big")
Show the answer
Answer: A, SyntaxError: expected ':'
Every
Run this code in the editorif,elifandelseline must end with a colon. Without it the program is rejected before it runs, and Python 3.13 says exactly what is missing:expected ':'.Question 14 · Easy
Which error does this code raise?
x = 10 if x > 5: print("big")
Show the answer
Answer: C, IndentationError: expected an indented block
The body of an
Run this code in the editorifmust be indented under it. Hereprintstarts at the left margin, so Python raisesIndentationError(a kind ofSyntaxError) before running anything.Question 15 · Medium
Which error does this code raise?
score = 40 if score >= 50: result = "pass" print(result)
Show the answer
Answer: D, NameError: name 'result' is not defined
Python does not check in advance that every branch creates
Run this code in the editorresult. Theifis false, so the assignment never runs andprint(result)raisesNameError.UnboundLocalErroronly happens inside a function.Question 16 · Easy
Which of these values counts as true in an
ifcondition?Show the answer
Answer: A, [0]
A list is truthy when it has at least one item, even if that item is 0. The number 0, the empty string
''andNoneare all falsy.Question 17 · Medium
In a
matchstatement, what doescase _:match?Show the answer
Answer: C, Any value that no earlier case matched
_is the wildcard pattern: it matches any value without storing it, so it works like theelseof anifchain. Place it last, since it would catch everything.Question 18 · Hard
A variable
limit = 10exists, and amatchstatement ends withcase limit:. What does that case do?Show the answer
Answer: D, It matches any value and stores it in limit
A bare name in a
caseis a capture pattern: it matches anything and assigns the value to that name, overwriting it. To compare with a variable, use a dotted name such asConfig.LIMIT, or a guard:case n if n == limit:.Question 19 · Medium
Which line prints
evenwhennis even andoddotherwise?Show the answer
Answer: B
n % 2 == 0is true for even numbers, and the conditional expression picks the matching word.n % 2alone is truthy for odd numbers, so the first line is reversed; the third prints nothing for odd numbers; the last is a syntax error.Question 20 · Easy
Which
ifline runs its block only when the listitemsis empty?Show the answer
Answer: B
An empty list is falsy, so
not itemsis true exactly when it is empty. An empty list is notNone,is []compares with a brand-new list object (always false), and=inside a condition is a syntax error.
Keep going
- Read the lesson behind these questions: Conditionals in the PythonBook
- Next topic: Python loops MCQ questions with answers
- Before this: Python string MCQ questions with answers
- Write the code yourself in the Python coding challenges, which check your solution with tests.