Quiz: Making Decisions — Conditionals and Boolean Logic
Test your understanding before moving on. Target: 70% or higher to proceed confidently.
Section 1: Multiple Choice (1 point each)
1. What does the following code print?
x = 10
if x > 5:
print("A")
elif x > 8:
print("B")
else:
print("C")
- A) A
- B) B
- C) A and B
- D) C
Answer
**A)** A *Why A:* `x > 5` is `True`, so `"A"` prints and the rest of the `elif-else` chain is skipped. Even though `x > 8` is also `True`, it's never evaluated because the first match wins. *Why not B:* The `elif` is only checked when the `if` condition is `False`. *Why not C:* In an `if-elif-else` chain, only one block executes. *Why not D:* `else` only runs when all preceding conditions are `False`. *Reference:* Section 4.42. What is the result of the Boolean expression True and False or True?
- A) True
- B) False
- C) Error
- D) None
Answer
**A)** True *Why A:* Precedence: `and` is evaluated before `or`. So `True and False` evaluates to `False`, then `False or True` evaluates to `True`. *Why not B:* `or True` at the end makes the result `True`. *Why not C:* This is valid Boolean syntax. *Reference:* Section 4.53. Which statement about short-circuit evaluation is TRUE?
- A) Python always evaluates both sides of
andandor - B) With
and, if the left side isFalse, the right side is not evaluated - C) With
or, if the left side isFalse, the right side is not evaluated - D) Short-circuit evaluation only applies to
not
Answer
**B)** With `and`, if the left side is `False`, the right side is not evaluated. *Why B:* If the left operand of `and` is `False`, the result must be `False` regardless of the right side, so Python skips it. *Why not A:* Short-circuit evaluation means Python sometimes skips the right side. *Why not C:* With `or`, it's the opposite — if the left is `True`, the right is skipped. *Why not D:* `not` has only one operand, so there's nothing to short-circuit. *Reference:* Section 4.64. What does the _ (underscore) represent in a match/case statement?
- A) A syntax error
- B) The variable that stores the matched value
- C) A wildcard pattern that matches anything not caught by previous cases
- D) A comment indicator
Answer
**C)** A wildcard pattern that matches anything not caught by previous cases. *Why C:* In `match/case`, `case _:` acts like the `else` in an `if-elif-else` chain — it's the default handler. *Why not A:* `_` is valid Python syntax in this context. *Why not B:* `_` doesn't store a value in pattern matching; it's a discard pattern. *Why not D:* Comments use `#`. *Reference:* Section 4.85. What is the output of this code?
value = 0
if value:
print("truthy")
else:
print("falsy")
- A) truthy
- B) falsy
- C) 0
- D) Error
Answer
**B)** falsy *Why B:* In Python, `0` is a "falsy" value — when used in a Boolean context, it evaluates to `False`. Other falsy values include `""` (empty string), `None`, and empty collections. *Why not A:* `0` is falsy, not truthy. *Why not D:* Using an integer in an `if` condition is valid Python. *Reference:* Section 4.2 (Boolean context)6. Which of the following correctly checks if day is either "Saturday" or "Sunday"?
- A)
if day == "Saturday" or "Sunday": - B)
if day == "Saturday" or day == "Sunday": - C)
if day == ("Saturday" or "Sunday"): - D)
if day = "Saturday" or day = "Sunday":
Answer
**B)** `if day == "Saturday" or day == "Sunday":` *Why B:* Each side of `or` must be a complete comparison expression. *Why not A:* `"Sunday"` alone is a non-empty string (always truthy), so this condition is always `True` regardless of `day`. *Why not C:* `("Saturday" or "Sunday")` evaluates to `"Saturday"` (short-circuit), so this only checks if `day == "Saturday"`. *Why not D:* `=` is assignment, not comparison. This is a `SyntaxError`. *Reference:* Section 4.57. What will this code do?
items = []
if len(items) > 0 and items[0] == "apple":
print("Found apple!")
else:
print("No apple.")
- A) Print "Found apple!"
- B) Print "No apple."
- C) Raise an IndexError
- D) Raise a TypeError
Answer
**B)** Print "No apple." *Why B:* `len(items) > 0` evaluates to `False` (the list is empty). Due to short-circuit evaluation, `items[0]` is never evaluated, preventing an `IndexError`. *Why not C:* Short-circuit evaluation prevents the `IndexError`. *Reference:* Section 4.6Section 2: True/False (1 point each)
8. An if-elif-else chain can have multiple elif blocks.
Answer
**True.** You can have as many `elif` blocks as you need between the `if` and the optional `else`. *Reference:* Section 4.49. The else clause in an if-else statement requires a condition.
Answer
**False.** `else` never takes a condition. It catches everything not caught by the preceding `if` and `elif` conditions. Writing `else condition:` is a `SyntaxError`. *Reference:* Section 4.910. In the expression not a and b, not is applied to a only, not to the entire expression a and b.
Answer
**True.** `not` has higher precedence than `and`, so `not a and b` is evaluated as `(not a) and b`. To negate the entire expression, use `not (a and b)`. *Reference:* Section 4.511. The match/case statement can be used to match ranges like score >= 90.
Answer
**False.** `match/case` matches against specific values or patterns, not ranges or inequalities. Use `if-elif-else` for range-based conditions. *Reference:* Section 4.812. If no condition in an if-elif chain (with no else) is True, nothing happens and the program continues after the chain.
Answer
**True.** Without an `else`, it's possible for none of the branches to execute. The program simply continues with the next statement after the chain. *Reference:* Section 4.4Section 3: Code Tracing (2 points each)
13. What does this code print?
a = 3
b = 7
c = 5
if a > b:
print("X")
elif c > b:
print("Y")
elif a + c > b:
print("Z")
else:
print("W")
Answer
**Z** Trace: `a > b` → `3 > 7` → `False`. `c > b` → `5 > 7` → `False`. `a + c > b` → `3 + 5 > 7` → `8 > 7` → `True` → prints `"Z"`. *Reference:* Section 4.414. What does this code print?
x = 15
y = 20
if x > 10 and y > 25:
print("both high")
elif x > 10 or y > 25:
print("one high")
elif x > 5:
print("x is moderate")
else:
print("both low")
Answer
**one high** Trace: `x > 10 and y > 25` → `True and False` → `False`. `x > 10 or y > 25` → `True or False` → `True` → prints `"one high"`. *Reference:* Sections 4.4, 4.515. What does this code print?
match "hello":
case "hi":
print("A")
case "hello" | "hey":
print("B")
case _:
print("C")
Answer
**B** The string `"hello"` doesn't match `"hi"`, but it does match the second case (`"hello" | "hey"` matches either value). `"B"` prints. *Reference:* Section 4.8Section 4: Short Answer (2 points each)
16. Rewrite this nested conditional as a flat if-elif-else chain:
if temperature > 100:
if is_humid:
print("Dangerous")
else:
print("Very hot")
else:
print("OK")
Answer
if temperature > 100 and is_humid:
print("Dangerous")
elif temperature > 100:
print("Very hot")
else:
print("OK")
The nested conditional is flattened by combining the outer and inner conditions with `and`.
*Reference:* Section 4.7
17. Explain why this code has a bug, and fix it:
score = 85
if score >= 60:
print("Pass")
elif score >= 80:
print("Merit")
elif score >= 90:
print("Distinction")
Answer
**Bug:** The conditions are ordered from least restrictive to most restrictive. A score of 85 matches `score >= 60` first, so it prints "Pass" instead of "Merit." A score of 95 would also print "Pass." **Fix:** Order from most restrictive to least restrictive:score = 85
if score >= 90:
print("Distinction")
elif score >= 80:
print("Merit")
elif score >= 60:
print("Pass")
else:
print("Fail")
*Reference:* Section 4.4 (Order Matters)
18. Write a single Boolean expression (using and, or, not) that evaluates to True when a variable age represents someone who is a teenager (13-19 inclusive).
Answer
age >= 13 and age <= 19
Or equivalently using Python's chained comparison:
13 <= age <= 19
Both evaluate to `True` for ages 13 through 19 inclusive.
*Reference:* Section 4.5
Section 5: Debugging (2 points each)
19. This code is supposed to check if a number is between 1 and 100 inclusive, but it has a bug. Find and fix it.
num = int(input("Enter a number: "))
if num > 1 or num < 100:
print("In range")
else:
print("Out of range")
Answer
**Bug:** The condition uses `or` instead of `and`. The expression `num > 1 or num < 100` is `True` for almost every number — e.g., `num = 500` satisfies `num > 1`, and `num = -50` satisfies `num < 100`. Also, `>` should be `>=` to be inclusive. **Fix:**num = int(input("Enter a number: "))
if num >= 1 and num <= 100:
print("In range")
else:
print("Out of range")
Or with chained comparison: `if 1 <= num <= 100:`
*Reference:* Section 4.5
20. This code should apply a 10% discount when total is over $50 AND the customer has a coupon. Find all the bugs.
total = input("Enter total: ")
has_coupon = input("Do you have a coupon? (yes/no): ")
if total > 50 and has_coupon == "yes"
discount = total * 0.10
total = total - discount
print(f"Discount applied! New total: ${total}")
else:
print(f"Total: {total}")
Answer
**Three bugs:** 1. **Missing type conversion:** `total = input(...)` returns a string, so `total > 50` raises a `TypeError`. Fix: `total = float(input("Enter total: "))` 2. **Missing colon:** The `if` line doesn't end with `:`. Fix: add `:` at the end of the `if` line. 3. **Formatting issue:** The `else` branch prints `{total}` without the `$`. Minor, but inconsistent with the `if` branch. **Fixed code:**total = float(input("Enter total: "))
has_coupon = input("Do you have a coupon? (yes/no): ")
if total > 50 and has_coupon == "yes":
discount = total * 0.10
total = total - discount
print(f"Discount applied! New total: ${total:.2f}")
else:
print(f"Total: ${total:.2f}")
*Reference:* Sections 4.2, 4.5, 4.9