Tracing assignments to find a variable's value

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

Tracing assignments to find a variable's value

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. X ← 5 then X ← X + 2. What is X after both lines?
A 7
B 5
2. Counter ← 0, then Counter ← Counter + 1 runs three times. What is Counter at the end?
A 1
B 3
3. How is a trace table set up?
A One column for each OUTPUT statement only
B One column for each variable
4. Total ← 10, Count ← 3, Total ← Total - Count, Count ← Count * 2. What are the final values?
A Total 7, Count 6
B Total 10, Count 6
5. N ← 17, Q ← N DIV 5, R ← N MOD 5. What are Q and R?
A Q = 3, R = 2
B Q = 2, R = 3

Match the Pairs

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

Left columnRight column
1Counter ← Counter + 1 AAdds 1 to the current value of Counter and stores the new value back in Counter. In a trace table, write the new value in the Counter column.
2Remainder ← 17 MOD 5 BRemainder gets the value 2 because 17 divided by 5 leaves 2. In a trace table, write 2 in the Remainder column.
3Trace the lines in order and record each variable's new value after every assignment. After Total ← 4 and then Total ← Total * 3, Total is 12. CThe student used the new value of X (10) to work out Y. Y was assigned when X was 5, so Y stays 7. Changing X later does not change Y.
4To trace DIV and MOD, work out each result on its own line. Starting with Number ← 47, Tens ← Number DIV 10 stores 4 and Units ← Number MOD 10 stores 7. DThe student only recorded one of the two additions. Counter goes 0, then 1, then 2, so the output is 2, not 1. Every assignment needs its own new value in the trace table.
5Counter ← 0 Counter ← Counter + 1 Counter ← Counter + 1 OUTPUT Counter // A student traces the output as 1 ETotal ← 4 Total ← Total * 3 OUTPUT Total
6X ← 5 Y ← X + 2 X ← 10 OUTPUT Y // A student traces the output as 12 FNumber ← 47 Tens ← Number DIV 10 Units ← Number MOD 10

Definitions

trace table

Fill in the Blanks

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

1.
Counter ← 0 Counter ← Counter + 1 OUTPUT Counter // shows _
2.
Counter ← 3 Counter ← Counter + 1 Counter ← Counter + 1 // now _
3.
Total ← 17 Tens ← Total ___ 10 // 1 Units ← Total MOD 10 // 7
4.
Total ← 17 Units ← Total MOD 5 OUTPUT Units // shows _
5.
A ← 4 B ← A * 2 A ← _ + 1 // A is now 9

Spot the Error

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

Question 1
A DECLARE Count : INTEGER
B Count ← 0
C Count + 1 ← Count
D OUTPUT Count
Question 2
A DECLARE Total INTEGER
B Total ← 5
C Total ← Total * 2
D OUTPUT Total
Question 3
A DECLARE Level : INTEGER
B Level ← 2
C Level ← Level + 3
D OUTPUT Level // shows 6
Question 4
A DECLARE Score : INTEGER
B Score = 10
C Score ← Score + 5
D OUTPUT Score
Question 5
A X ← 4
B Y ← X + 3 // Y is 12
C X ← Y - 2
D OUTPUT X, Y

Answer Key

Tracing assignments to find a variable's value
Section A: Quick Questions
Q1
A — 7
Q2
B — 3
Q3
B — One column for each variable
Q4
A — Total 7, Count 6
Q5
A — Q = 3, R = 2
Section B: Match the Pairs
Q1
Counter ← Counter + 1 → Adds 1 to the current value of Counter and stores the new value back in Counter. In a trace table, write the new value in the Counter column.
Q2
Remainder ← 17 MOD 5 → Remainder gets the value 2 because 17 divided by 5 leaves 2. In a trace table, write 2 in the Remainder column.
Q3
Trace the lines in order and record each variable's new value after every assignment. After Total ← 4 and then Total ← Total * 3, Total is 12. → Total ← 4 Total ← Total * 3 OUTPUT Total
Q4
To trace DIV and MOD, work out each result on its own line. Starting with Number ← 47, Tens ← Number DIV 10 stores 4 and Units ← Number MOD 10 stores 7. → Number ← 47 Tens ← Number DIV 10 Units ← Number MOD 10
Q5
Counter ← 0 Counter ← Counter + 1 Counter ← Counter + 1 OUTPUT Counter // A student traces the output as 1 → The student only recorded one of the two additions. Counter goes 0, then 1, then 2, so the output is 2, not 1. Every assignment needs its own new value in the trace table.
Q6
X ← 5 Y ← X + 2 X ← 10 OUTPUT Y // A student traces the output as 12 → The student used the new value of X (10) to work out Y. Y was assigned when X was 5, so Y stays 7. Changing X later does not change Y.
Section C: Definitions
Q1
trace table — A table with one column per variable. Follow the program line by line and write each variable's new value after every assignment, so you can predict the final OUTPUT.
Section D: Fill in the Blanks
Q1
Counter ← 0 Counter ← Counter + 1 OUTPUT Counter // shows 1
Q2
Counter ← 3 Counter ← Counter + 1 Counter ← Counter + 1 // now 5
Q3
Total ← 17 Tens ← Total DIV 10 // 1 Units ← Total MOD 10 // 7
Q4
Total ← 17 Units ← Total MOD 5 OUTPUT Units // shows 2
Q5
A ← 4 B ← A * 2 A ← B + 1 // A is now 9
Section E: Spot the Error
Q1
C — An assignment puts the variable on the left of the arrow and the expression on the right. When tracing, Count becomes 0 and then 1. The line should be Count ← Count + 1.
Q2
A — DECLARE needs a colon between the identifier and its data type. It should be DECLARE Total : INTEGER. After the later lines, the trace table shows Total as 5, then 10.
Q3
C — The trace table gives Level = 2, then 2 * 3 = 6, which matches the comment. Level + 3 would give 5. The line should be Level ← Level * 3.
Q4
B — Cambridge pseudocode assigns a value with the ← arrow, not =. The line should be Score ← 10. Then the trace shows Score as 10 and then 15.
Q5
B — X is 4, so X + 3 gives 7, not the 12 in the comment. The trace needs Y ← X * 3 to give 12. X then becomes 10 on the next line.