Testing compound conditions with and, or, not

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Testing compound conditions with and, or, not

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A program checks: if age >= 13 and age <= 19. Which test inputs make EACH part True and False in turn?
A age = 14, 15 and 16
B age = 10, 15 and 25
2. A compound condition has two parts joined by and. How many True/False combinations are in its truth table?
A 2
B 4
3. For the condition: has_ticket and has_id, when is the result True?
A Only when both parts are True
B When at least one part is True
4. A student wants the discount if a customer is a member OR spends over $50. A tester uses member = False and spend = 20. What result should the truth table give?
A False
B True
5. A program should only open the door if code_ok is True and time_ok is True, but the code uses or. Which test finds the error?
A code_ok = True, time_ok = False (door should stay shut)
B code_ok = True, time_ok = True (door should open)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1age >= 13 and age <= 19 AA range condition that is only True when both parts are True, so test 12, 13, 19 and 20 to check each boundary
2day == "Sat" or day == "Sun" BTrue and False
3A truth table row tests one combination of the parts. For A=True and B=False, the and result should be False. CUsing or instead of and makes the condition True for every number, so even 5 or 50 pass. A test with age 5 would expose the error. It should use and.
4Using not flips a result, so a test must check that True becomes False and False becomes True DThe boundary values 13 and 19 are wrongly rejected because > and < were used. It should be >= and <=. Testing 13 and 19 finds the error.
5if age >= 13 or age <= 19: Enot (score > 50)
6if age > 13 and age < 19: FA compound condition that is True when at least one part is True, so it is False only when both parts are False

Definitions

truth table

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
print(True ___ False) # False
2.
age = 17 if age >= 13 and age __ 17: print("Teen")
3.
raining = True if ___ raining: print("Walk")
4.
age = 13 ok = age >= 13 and age <= 17 print(__)
5.
score = 120 if score < 0 __ score > 100: print("Invalid")

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A for a in [True, True]:
B for b in [True, False]:
C print(a, b, a and b)
Question 2
A sunny = True
B warm = False
C if sunny or warm:
D print('Beach')
Question 3
A x = 10
B lo = 5
C hi = 10
D ok = lo <= x and x < hi
E print(ok)
Question 4
A p = True
B q = False
C result = not (p and q)
D assert result == False
Question 5
A is_weekend = False
B is_holiday = True
C if is_weekend and is_holiday:
D print('No school')

Answer Key

Testing compound conditions with and, or, not
Section A: Quick Questions
Q1
B — age = 10, 15 and 25
Q2
B — 4
Q3
A — Only when both parts are True
Q4
A — False
Q5
A — code_ok = True, time_ok = False (door should stay shut)
Section B: Match the Pairs
Q1
age >= 13 and age <= 19 → A range condition that is only True when both parts are True, so test 12, 13, 19 and 20 to check each boundary
Q2
day == "Sat" or day == "Sun" → A compound condition that is True when at least one part is True, so it is False only when both parts are False
Q3
A truth table row tests one combination of the parts. For A=True and B=False, the and result should be False. → True and False
Q4
Using not flips a result, so a test must check that True becomes False and False becomes True → not (score > 50)
Q5
if age >= 13 or age <= 19: → Using or instead of and makes the condition True for every number, so even 5 or 50 pass. A test with age 5 would expose the error. It should use and.
Q6
if age > 13 and age < 19: → The boundary values 13 and 19 are wrongly rejected because > and < were used. It should be >= and <=. Testing 13 and 19 finds the error.
Section C: Definitions
Q1
truth table — A table listing every True/False combination of the parts in an and/or/not condition. Use it to pick test inputs that make each part True and False, then check each result.
Section D: Fill in the Blanks
Q1
print(True and False) # False
Q2
age = 17 if age >= 13 and age <= 17: print("Teen")
Q3
raining = True if not raining: print("Walk")
Q4
age = 13 ok = age >= 13 and age <= 17 print(ok)
Q5
score = 120 if score < 0 or score > 100: print("Invalid")
Section E: Spot the Error
Q1
A — A truth table needs each part to be True and False in turn. [True, True] never tests a as False, so half the rows are missing. It should be [True, False].
Q2
C — The beach needs both parts True, so the condition must use and. With or, sunny True and warm False wrongly gives True. It should be if sunny and warm:.
Q3
D — The range includes the upper boundary, so 10 should give True. Using < excludes hi and the boundary test fails. It should be x <= hi.
Q4
D — The truth table says True and False gives False, so not (False) is True. The assert should expect True, not False.
Q5
C — No school should happen if either part is True, so the condition needs or. With and, a holiday on a weekday would wrongly print nothing. It should be if is_weekend or is_holiday:.