Working out the expected output before you run the code

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

Working out the expected output before you run the code

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A spec says: 'Add 10% GST to the price.' For a price of $50, how should you get the expected output?
A Run the program and write down whatever number it prints
B Calculate 50 x 1.10 = 55 by hand from the spec before running the code
2. When should you work out the expected output of a test case?
A Before you run the code
B After you see what the program prints
3. Which is the source of truth for what a program should do?
A The problem specification
B The program's own output
4. Mia's program has a bug and prints 18 for the average of 10 and 20. She copies 18 into the Expected column. What does this show?
A The test proves nothing because the bug is now hidden inside the expected output
B The test passes, so the program is correct
5. A spec says: 'Students get a pass if their mark is 50 or more.' What is the expected output for a mark of 50?
A Fail, because the program printed Fail
B Pass, because the spec says 50 or more

Match the Pairs

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

Left columnRight column
1expected = 15 * 0.10 # spec: 10% discount AThe expected output was taken from running the program rather than calculated from the specification beforehand.
2expected = discount(15) # copied from program BThe expected value is set equal to the program's own output, so the test always passes and proves nothing.
3The specification is the source of truth: it says 3 items at $4 each must total $12, so that is the expected output. Cexpected = 12 actual = total(3, 4) print(expected == actual)
4Work out the expected output by hand before running the code, then compare it to the actual output. DThe expected value was copied from the program's own output, so the test proves nothing.
5actual = area(4, 5) expected = actual print(expected == actual) Eexpected = 3 * 4 # spec says $4 each print(expected) # 12
6# Run the program first, see it print 7 expected = 7 FThe expected output is worked out by hand from the specification before the program is run.

Definitions

expected output

Fill in the Blanks

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

1.
# Spec: GST is 10% of the price ________ = 5 # 10% of 50, worked out by hand
2.
expected = 5 # worked out by hand actual = gst(50) print(expected __ actual)
3.
# Spec: total = price * quantity # Test: 3 items at $4 each expected = __
4.
# Spec: 20% off a $50 item # By hand: 50 - 10 = 40 expected = __
5.
expected = 12 # from the spec actual = total(3, 4) __ actual == expected: print("PASS")

Spot the Error

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

Question 1
A # Spec: cost = price * qty
B price, qty = 4, 3
C expected = price * qty
D actual = price * qty
E print(expected == actual)
Question 2
A expected = discount(20)
B actual = discount(20)
C if expected == actual:
D print('PASS')
Question 3
A # Spec: area = width * height
B def area(w, h):
C return w * h
D expected = 3 + 4
E print(area(3, 4) == expected)
Question 4
A # Spec: tax is 10% of income
B expected = 500 * 0.01
C actual = tax(500)
D print(expected, actual)
Question 5
A # Spec: 15% off if over 100
B price = 120
C expected = 120 * 0.15
D actual = final(price)
E print(expected == actual)

Answer Key

Working out the expected output before you run the code
Section A: Quick Questions
Q1
B — Calculate 50 x 1.10 = 55 by hand from the spec before running the code
Q2
A — Before you run the code
Q3
A — The problem specification
Q4
A — The test proves nothing because the bug is now hidden inside the expected output
Q5
B — Pass, because the spec says 50 or more
Section B: Match the Pairs
Q1
expected = 15 * 0.10 # spec: 10% discount → The expected output is worked out by hand from the specification before the program is run.
Q2
expected = discount(15) # copied from program → The expected value was copied from the program's own output, so the test proves nothing.
Q3
The specification is the source of truth: it says 3 items at $4 each must total $12, so that is the expected output. → expected = 3 * 4 # spec says $4 each print(expected) # 12
Q4
Work out the expected output by hand before running the code, then compare it to the actual output. → expected = 12 actual = total(3, 4) print(expected == actual)
Q5
actual = area(4, 5) expected = actual print(expected == actual) → The expected value is set equal to the program's own output, so the test always passes and proves nothing.
Q6
# Run the program first, see it print 7 expected = 7 → The expected output was taken from running the program rather than calculated from the specification beforehand.
Section C: Definitions
Q1
expected output — The correct result of a test case, worked out by hand from the specification before running the code. Never copy it from the program's own output, or the test proves nothing.
Section D: Fill in the Blanks
Q1
# Spec: GST is 10% of the price expected = 5 # 10% of 50, worked out by hand
Q2
expected = 5 # worked out by hand actual = gst(50) print(expected == actual)
Q3
# Spec: total = price * quantity # Test: 3 items at $4 each expected = 12
Q4
# Spec: 20% off a $50 item # By hand: 50 - 10 = 40 expected = 40
Q5
expected = 12 # from the spec actual = total(3, 4) if actual == expected: print("PASS")
Section E: Spot the Error
Q1
C — The expected value was calculated with the program's own formula, so the test can never fail. Work it out by hand from the spec: 4 * 3 = 12, so it should be expected = 12.
Q2
A — The expected output must be worked out by hand (10% of 20 is 2.0 or 20 * 0.10), not taken from the function being tested. Using discount(20) just compares the function with itself.
Q3
D — The hand calculation does not follow the spec. The spec says area = width * height, so the expected value is 3 * 4 = 12, not 3 + 4 = 7.
Q4
B — The spec says tax is 10%, which is 0.10. Using 0.01 gives the wrong expected output (5.0 instead of 50.0), so the test would wrongly fail correct code.
Q5
C — The hand calculation gives the discount amount, not the final price. A 15% discount means the customer pays 85%, so expected = 120 * 0.85 = 102.