Editor

Python if-else MCQ questions with answers

Which branch runs, and why: if, elif, else, truthiness and match.

0 of 20 answered

  1. 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 >= 90 but passes >= 70, so it prints B and skips the rest, even though >= 50 is also true.

    Run this code in the editor
  2. 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 if statements, not an if/elif chain, so both are checked. 15 > 10 prints big, then 15 > 5 prints medium; the else belongs only to the second if.

    Run this code in the editor
  3. 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

    0, the empty string, the empty list and None are all falsy. The string "0" is not empty and the list [0] has one item, so both are truthy, whatever they contain.

    Run this code in the editor
  4. 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

    None is falsy, so the first branch is skipped. None == 0 is False because None is not a number, so the else branch prints no.

    Run this code in the editor
  5. 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 A if condition else B gives A when the condition is true and B otherwise. 17 >= 18 is false, so status is "minor".

    Run this code in the editor
  6. 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 if runs; 7 % 2 is 1, so its else prints big odd. The last print is not indented, so it runs after the whole if statement, whichever branch was taken.

    Run this code in the editor
  7. 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. n > 5 is false, so the whole indented block, including the nested if, is skipped and else prints C. The last if is a separate statement, and 4 > 2 prints D.

    Run this code in the editor
  8. 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

    choice == 1 or 2 means (choice == 1) or 2. The comparison is False, so or returns its second operand, 2, which is truthy, and the if always runs. The correct test is choice == 1 or choice == 2, or choice in (1, 2).

    Run this code in the editor
  9. 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

    match runs only the first case that matches; there is no fall-through to later cases. "stop" | "halt" matches either value, so it prints Stopping and the case _ default is skipped.

    Run this code in the editor
  10. 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

    (0, 0) fails because the second item is 5. In case (0, y):, the 0 must match exactly while the name y captures whatever is in that position, so y becomes 5 and that case runs.

    Run this code in the editor
  11. 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 warm and the chain ends there; temp > 30 is never checked. When conditions overlap, put the strictest one (> 30) first.

    Run this code in the editor
  12. 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 if/elif/else: n > 0 is false, so Python evaluates "negative" if n < 0 else "zero", and 0 < 0 is also false, giving zero.

    Run this code in the editor
  13. Question 13 · Easy

    Which error does this code raise?

    x = 10
    if x > 5
        print("big")
    Show the answer

    Answer: A, SyntaxError: expected ':'

    Every if, elif and else line must end with a colon. Without it the program is rejected before it runs, and Python 3.13 says exactly what is missing: expected ':'.

    Run this code in the editor
  14. 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 if must be indented under it. Here print starts at the left margin, so Python raises IndentationError (a kind of SyntaxError) before running anything.

    Run this code in the editor
  15. 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 result. The if is false, so the assignment never runs and print(result) raises NameError. UnboundLocalError only happens inside a function.

    Run this code in the editor
  16. Question 16 · Easy

    Which of these values counts as true in an if condition?

    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 '' and None are all falsy.

  17. Question 17 · Medium

    In a match statement, what does case _: 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 the else of an if chain. Place it last, since it would catch everything.

  18. Question 18 · Hard

    A variable limit = 10 exists, and a match statement ends with case 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 case is 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 as Config.LIMIT, or a guard: case n if n == limit:.

  19. Question 19 · Medium

    Which line prints even when n is even and odd otherwise?

    Show the answer

    Answer: B

    n % 2 == 0 is true for even numbers, and the conditional expression picks the matching word. n % 2 alone is truthy for odd numbers, so the first line is reversed; the third prints nothing for odd numbers; the last is a syntax error.

  20. Question 20 · Easy

    Which if line runs its block only when the list items is empty?

    Show the answer

    Answer: B

    An empty list is falsy, so not items is true exactly when it is empty. An empty list is not None, is [] compares with a brand-new list object (always false), and = inside a condition is a syntax error.

Keep going