Tracing assignments and arithmetic

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

Tracing assignments and arithmetic

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Counter is 4. The line Counter ← Counter + 1 runs. What do you write in the Counter column?
A 5
B 1
2. Total is 10 and Value is 7. The line Total ← Total + Value runs. What is the new Total?
A 7
B 17
3. Which order is correct when you trace an assignment?
A Work out the right side using the old values, then store the result
B Store the new value first, then work out the right side
4. Counter starts at 0. Counter ← Counter + 1 runs three times. What is Counter at the end?
A 3
B 1
5. In Total ← Total + Value, which column changes when the line runs?
A Both Total and Value change.
B Only Total changes. Value stays the same.

Match the Pairs

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

Left columnRight column
1Counter ← Counter + 1 ATake the current value of Counter, add 1, then store the new value back in the Counter column of the trace table.
2Total ← Total + Value BThe student ignored the old value of Counter and only used the 1. They should work out 3 + 1 and write 4.
3Counter is 4 before this line runs. Working out the right side gives 4 + 1, so 5 is written in the Counter column. CAdd the current Value to the current Total, then write the answer in the Total column as the new running total.
4Total is 20 and Value is 7. Work out 20 + 7 by hand, then write 27 in the Total column. Value stays 7. DTotal ← Total + Value
5Counter is 3. For Counter ← Counter + 1 the student writes 1 in the Counter column. EThe student replaced Total with Value instead of adding them. The correct sum is 12 + 5, so 17 goes in the Total column.
6Total is 12 and Value is 5. For Total ← Total + Value the student writes 5 in the Total column. FCounter ← Counter + 1

Definitions

Counter ← Counter + 1

Fill in the Blanks

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

1.
Counter ← 0 Counter ← _______ + 1
2.
Total ← 10 Total ← Total + 5 // Total is now __
3.
Total ← 0 Total ← Total _ Value
4.
Counter ← 4 Counter ← Counter + 1 // Old value was _, new value is 5
5.
Total ← 12 Value ← 7 _____ ← Total + Value

Spot the Error

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

Question 1
A 0 ← Counter
B Counter ← Counter + 1
C Counter ← Counter + 1
D OUTPUT Counter
Question 2
A Total ← 0
B Value ← 8
C Total ← Total + Value
D OUTPUT Value
Question 3
A Counter ← 5
B Counter = Counter + 1
C OUTPUT Counter
Question 4
A Total ← 10
B Total ← Totl + 5
C OUTPUT Total
Question 5
A DECLARE Count : INTEGER
B Count ← "0"
C Count ← Count + 1
D OUTPUT Count

Answer Key

Tracing assignments and arithmetic
Section A: Quick Questions
Q1
A — 5
Q2
B — 17
Q3
A — Work out the right side using the old values, then store the result
Q4
A — 3
Q5
B — Only Total changes. Value stays the same.
Section B: Match the Pairs
Q1
Counter ← Counter + 1 → Take the current value of Counter, add 1, then store the new value back in the Counter column of the trace table.
Q2
Total ← Total + Value → Add the current Value to the current Total, then write the answer in the Total column as the new running total.
Q3
Counter is 4 before this line runs. Working out the right side gives 4 + 1, so 5 is written in the Counter column. → Counter ← Counter + 1
Q4
Total is 20 and Value is 7. Work out 20 + 7 by hand, then write 27 in the Total column. Value stays 7. → Total ← Total + Value
Q5
Counter is 3. For Counter ← Counter + 1 the student writes 1 in the Counter column. → The student ignored the old value of Counter and only used the 1. They should work out 3 + 1 and write 4.
Q6
Total is 12 and Value is 5. For Total ← Total + Value the student writes 5 in the Total column. → The student replaced Total with Value instead of adding them. The correct sum is 12 + 5, so 17 goes in the Total column.
Section C: Definitions
Q1
Counter ← Counter + 1 — When tracing, work out the right side using the current values, then write the result in that variable's column. The old value is used first, then replaced by the new one.
Section D: Fill in the Blanks
Q1
Counter ← 0 Counter ← Counter + 1
Q2
Total ← 10 Total ← Total + 5 // Total is now 15
Q3
Total ← 0 Total ← Total + Value
Q4
Counter ← 4 Counter ← Counter + 1 // Old value was 4, new value is 5
Q5
Total ← 12 Value ← 7 Total ← Total + Value
Section E: Spot the Error
Q1
A — The variable being assigned to must be on the left of the arrow. It should be Counter ← 0, so the trace table starts with Counter as 0.
Q2
D — The program should output Total, which holds the running sum (8 after the trace). Value only holds the number that was added. It should be OUTPUT Total.
Q3
B — In Cambridge pseudocode, = is a comparison, not an assignment. Use the arrow: Counter ← Counter + 1. In a trace, the new value 6 replaces the old 5.
Q4
B — The variable name is misspelled. Totl does not exist, so there is no current value to use. It should be Total ← Total + 5, which gives 15.
Q5
B — Count is declared as an INTEGER, so it cannot be given a STRING. The quotes make "0" text. It should be Count ← 0.