Checking a trace against the real program output

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

Checking a trace against the real program output

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. You traced a program using the input 5. Which input should you use when you run the real program to check your trace?
A A different input, like 8, to test more
B The same input, 5
2. Your trace says the program prints 12. The real program prints 12 as well. What does this tell you?
A The program is proven correct for every input
B For this input, the trace and the code agree
3. Your trace says the output is 10, but the real program prints 14. What should you do first?
A Re-trace the code line by line to find the difference
B Assume the computer is wrong and ignore the output
4. A mismatch between your trace and the real output means the code must have a bug.
A False: the trace might have a slip
B True: the code is always the problem
5. You re-trace and find you wrote total = 3 + 4 as 8 in your table. Which kind of mistake is this?
A A tracing slip, because the code is fine
B A code error, because the output differed

Match the Pairs

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

Left columnRight column
1print(total) # trace said 15, output 12 AThe program was run with a different input (5) than the one traced (3), so the output cannot be compared with the trace
2n = int(input()) # trace used n = 4, run with 4 BThe real output differs from the trace, so either the trace or the code has a mistake and needs re-tracing
3If the trace shows 10 but the program prints 10, the trace and the code agree, so no mistake is found for that input CRun the program with the same input you used in the trace so the two can be compared fairly
4If the output differs from the trace, re-trace the code line by line to see where the trace and the program split apart DAssuming the code is wrong without checking the trace; a mismatch could be a tracing slip, so re-trace before changing the code
5# traced with n = 3 n = 5 print(n * 2) # compare with trace E# trace: total = 10 print(total) # output: 10
6# trace: 8, output: 6 # so the code must be wrong print(result) F# trace: x = 6, output: x = 5 # re-trace from the first line

Definitions

trace vs output

Fill in the Blanks

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

1.
expected = 12 actual = sum([2, 4, 6]) print(expected __ actual)
2.
traced = 12 output = 10 if traced != ______:
3.
n = 5 print(_ * 2) print(n + 1)
4.
msg = "OK" if trace == output ____ "Retrace"
5.
def double(n): ______ n * 2 print(double(4))

Spot the Error

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

Question 1
A x = 4
B total = x + 3
C expected = 12
D print(total == expected)
Question 2
A nums = [2, 4, 6]
B total = 0
C for n in nums:
D total = n
E print(total)
Question 3
A expected = 10
B actual = 3 + "7"
C if actual == expected:
D print("Match")
Question 4
A x = 50
B y = x * 2
C print(y)
D # trace: 10
Question 5
A trace = 6
B real = 2 * 3
C if real = trace:
D print("Same")
E else:
F print("Re-trace")

Answer Key

Checking a trace against the real program output
Section A: Quick Questions
Q1
B — The same input, 5
Q2
B — For this input, the trace and the code agree
Q3
A — Re-trace the code line by line to find the difference
Q4
A — False: the trace might have a slip
Q5
A — A tracing slip, because the code is fine
Section B: Match the Pairs
Q1
print(total) # trace said 15, output 12 → The real output differs from the trace, so either the trace or the code has a mistake and needs re-tracing
Q2
n = int(input()) # trace used n = 4, run with 4 → Run the program with the same input you used in the trace so the two can be compared fairly
Q3
If the trace shows 10 but the program prints 10, the trace and the code agree, so no mistake is found for that input → # trace: total = 10 print(total) # output: 10
Q4
If the output differs from the trace, re-trace the code line by line to see where the trace and the program split apart → # trace: x = 6, output: x = 5 # re-trace from the first line
Q5
# traced with n = 3 n = 5 print(n * 2) # compare with trace → The program was run with a different input (5) than the one traced (3), so the output cannot be compared with the trace
Q6
# trace: 8, output: 6 # so the code must be wrong print(result) → Assuming the code is wrong without checking the trace; a mismatch could be a tracing slip, so re-trace before changing the code
Section C: Definitions
Q1
trace vs output — Run the code with the same input you traced, then compare. If the real output differs from your trace, either the trace or the code has a mistake, so re-trace.
Section D: Fill in the Blanks
Q1
expected = 12 actual = sum([2, 4, 6]) print(expected == actual)
Q2
traced = 12 output = 10 if traced != output:
Q3
n = 5 print(n * 2) print(n + 1)
Q4
msg = "OK" if trace == output else "Retrace"
Q5
def double(n): return n * 2 print(double(4))
Section E: Spot the Error
Q1
B — The trace gave 12, which comes from multiplying 4 by 3. The code adds instead, so the real output is 7 and does not match the trace. The line should be total = x * 3.
Q2
D — The trace shows total building up to 12. This line replaces total each time, so the real output is 6 and does not match the trace. It should add: total = total + n.
Q3
B — Adding the integer 3 to the string "7" causes a TypeError, so there is no real output to compare with the trace. The 7 should be a number: actual = 3 + 7.
Q4
A — The trace was done with x = 5. Running the code with a different input gives 100, so the comparison is meaningless. Use the same input for both: x = 5.
Q5
C — A single = assigns a value and is a syntax error in an if condition. To compare the real output with the trace, use ==: if real == trace:.