What it means to validate an algorithm or program

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

What it means to validate an algorithm or program

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which best describes what it means to validate a program?
A Check that it starts and finishes without showing an error message
B Prove it gives the correct output for its problem by testing it against test cases
2. A program runs with no error messages. Is it proven correct?
A No, it could still give wrong answers (a logic error)
B Yes, no errors means the program is validated
3. A program should find the average of 3 marks. It runs and prints (a + b + c) / 2 for 60, 70, 80. Which is true?
A It passes validation because it printed a number without crashing
B It ran, but the output 105 is wrong. The expected average is 70, so it fails validation
4. Which is an example of validating a program (not validating input data)?
A Running a grade calculator with test cases and comparing results to expected outputs
B Using a while loop to reject an age that is below 0
5. A student tests a program with only one test case and it passes. What can they conclude?
A Only that it works for that one case. A program is only as validated as its tests
B The whole program is fully validated and will work for all inputs

Match the Pairs

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

Left columnRight column
1assert average([2, 4, 6]) == 4 AThis only checks the input data. It does not prove the program produces the correct output for the problem.
2if age >= 0 and age <= 120: BRunning without errors is not proof of correctness. The output must be checked against expected results from test cases.
3A program can run with no errors and still be wrong, so you must compare its output with the expected output Cdef total(a, b): return a * b print(total(2, 3)) # expected 5, got 6
4A program is only as validated as its tests: passing every test in a weak set proves little DValidates one case: checks the function gives the expected output of 4 for this test input
5print('No errors, so the program is valid!') Etests = [(0, 0)] for x, want in tests: print(double(x) == want)
6if int(age) > 0: # validates my program FValidates input data, not the program: it checks the value is sensible before it is used

Definitions

validate a program

Fill in the Blanks

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

1.
def double(n): return n * 2 print(double(4) __ 8)
2.
if actual == expected: print("PASS") ____: print("FAIL")
3.
def double(n): return n * 2 ______ double(3) == 6
4.
tests = [(2, 4), (5, 10)] ___ n, want in tests: print(double(n) == want)
5.
tests = [(2, 4), (5, 10), (0, 0)] print(___(tests), "test cases")

Spot the Error

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

Question 1
A def area(w, h):
B return w + h
C print(area(3, 4))
D # expected output: 12
Question 2
A expected = 12
B actual = expected
C print(actual == expected)
Question 3
A def is_even(n):
B return n % 2 == 1
C print(is_even(6))
D print(is_even(7))
Question 4
A tests = [(2, 4), (3, 6)]
B for x, want in tests:
C print(double(x) == x)
Question 5
A tests = [(0, 0), (5, 10)]
B for n, want in tests:
C print(square(n) == want)

Answer Key

What it means to validate an algorithm or program
Section A: Quick Questions
Q1
B — Prove it gives the correct output for its problem by testing it against test cases
Q2
A — No, it could still give wrong answers (a logic error)
Q3
B — It ran, but the output 105 is wrong. The expected average is 70, so it fails validation
Q4
A — Running a grade calculator with test cases and comparing results to expected outputs
Q5
A — Only that it works for that one case. A program is only as validated as its tests
Section B: Match the Pairs
Q1
assert average([2, 4, 6]) == 4 → Validates one case: checks the function gives the expected output of 4 for this test input
Q2
if age >= 0 and age <= 120: → Validates input data, not the program: it checks the value is sensible before it is used
Q3
A program can run with no errors and still be wrong, so you must compare its output with the expected output → def total(a, b): return a * b print(total(2, 3)) # expected 5, got 6
Q4
A program is only as validated as its tests: passing every test in a weak set proves little → tests = [(0, 0)] for x, want in tests: print(double(x) == want)
Q5
print('No errors, so the program is valid!') → Running without errors is not proof of correctness. The output must be checked against expected results from test cases.
Q6
if int(age) > 0: # validates my program → This only checks the input data. It does not prove the program produces the correct output for the problem.
Section C: Definitions
Q1
validate a program — To prove a program gives the correct output for its problem by running or tracing it against test cases. Running without errors is not proof; it is only as validated as its tests.
Section D: Fill in the Blanks
Q1
def double(n): return n * 2 print(double(4) == 8)
Q2
if actual == expected: print("PASS") else: print("FAIL")
Q3
def double(n): return n * 2 assert double(3) == 6
Q4
tests = [(2, 4), (5, 10)] for n, want in tests: print(double(n) == want)
Q5
tests = [(2, 4), (5, 10), (0, 0)] print(len(tests), "test cases")
Section E: Spot the Error
Q1
B — The program runs without errors, but it prints 7 instead of the expected 12. Running without errors is not proof of correctness. It should be return w * h.
Q2
B — The actual output must come from running the program. Copying expected into actual means the check is always True and proves nothing. It should be actual = area(3, 4).
Q3
B — This returns True for odd numbers, so it gives the wrong output even though it runs fine. Testing against 6 and 7 would expose it. It should be n % 2 == 0.
Q4
C — The output must be compared with the expected output (want), not with the input x. It should be double(x) == want.
Q5
A — The expected output is wrong: 5 squared is 25, not 10. A correct program would fail this test. Expected values must be worked out correctly first.