REVISION GUIDE · PAPER 2

How to answer trace table questions

A step-by-step method for Cambridge IGCSE 0478 and AS and A Level 9618, with a worked example and practice question.

Trace table questions appear in almost every Cambridge Computer Science Paper 2, for both IGCSE 0478 and AS and A Level 9618. They are some of the easiest marks on the paper once you have a reliable method, and some of the easiest to throw away if you rush. This guide gives you a step-by-step method, a fully worked example and a practice question to try.

What a trace table question looks like

You are given an algorithm, usually as pseudocode or a flowchart, plus some input data. You are also given an empty table with a column for each variable and a column headed OUTPUT. Your job is to dry-run the algorithm by hand and record how the values change. Examiners use these questions to check that you can read code exactly as a computer would, not as you expect it to behave.

The 7-step method

  1. Read the whole algorithm first. Find the loops, the conditions and every line that changes a variable or produces output.
  2. Write the starting values. Record any variables set before the loop (for example Total ← 0) on the first row.
  3. Follow one line at a time. Put your finger or pencil on the current line. Never jump ahead because you think you know the answer.
  4. Write a value only when it changes. Leave the cell blank when a variable keeps its old value. Copying values down wastes time and can confuse the examiner.
  5. Check every condition carefully. Is it > or >=? Does the loop stop before or after the last value? Most lost marks come from boundary values like 50 in “Mark >= 50”.
  6. Record every output, exactly. Write outputs in the OUTPUT column on the row where they happen, including text and spacing if shown.
  7. Check the loop count. A FOR loop from 1 TO 5 runs exactly 5 times. A WHILE loop checks the condition before each pass; a REPEAT loop always runs at least once.
Examiner tip: start a new row each time the loop starts again, or whenever a variable changes after another has already changed on the current row. Keep the table moving down the page in the same order as the code runs.

Worked example

This algorithm reads five marks, counts how many are passes (50 or more) and adds up the passing marks.

Total ← 0
Count ← 0
FOR Index ← 1 TO 5
   INPUT Mark
   IF Mark >= 50
      THEN
         Total ← Total + Mark
         Count ← Count + 1
   ENDIF
NEXT Index
OUTPUT "Passed: ", Count, " Total: ", Total

Input data: 45, 72, 50, 38, 91

IndexMarkTotalCountOUTPUT
00
145
272721
3501222
438
5912133
Passed: 3 Total: 213

Notice three things. First, the starting values 0 and 0 are on their own row before the loop. Second, when the mark is 45 or 38 the condition is false, so Total and Count stay blank on those rows. Third, the mark of exactly 50 is counted because the condition is >=. Writing 72 instead of 122 on row 3, or missing the 50, are the two most common mistakes on questions like this.

Mistakes that cost marks

  • Running the loop one time too many or too few, especially with WHILE loops where the counter changes at the end of the loop body.
  • Using the old value in a calculation. In Total ← Total + Mark, use the value of Total from the row above, not the starting value.
  • Forgetting integer division. DIV and MOD give whole numbers: 17 DIV 5 is 3 and 17 MOD 5 is 2.
  • Missing outputs inside the loop, or writing the final output on the wrong row.
  • Mixing up string and number comparisons, for example comparing “10” and “9” as text.

Try it yourself

Number ← 1
Result ← 0
WHILE Number <= 10
   IF Number MOD 3 = 0
      THEN
         Result ← Result + Number
   ENDIF
   Number ← Number + 2
ENDWHILE
OUTPUT Result

Complete a trace table with the columns Number, Result and OUTPUT.

Show the answer
NumberResultOUTPUT
10
3
3
5
7
9
12
11
12

Number takes the values 1, 3, 5, 7, 9 and 11. Only 3 and 9 are multiples of 3, so Result becomes 3 and then 12. When Number reaches 11 the condition Number <= 10 is false, the loop stops and 12 is output.

Practise more

Use the trace table stepper and more exam-style questions in these units:

Frequently asked questions

What is a trace table?

A trace table is a grid with one column for each variable and output. You fill it in as you follow an algorithm line by line, to show how values change and what the algorithm outputs.

Do I need to fill in every cell of a trace table?

No. In Cambridge exams you only write a value when a variable changes. Leave a cell blank when the value stays the same, and do not copy values down unless the question asks you to.

How many marks are trace table questions worth?

In IGCSE 0478 Paper 2 and A Level 9618 Paper 2 they are usually worth 4 to 7 marks. Marks are often given per correct column or group of rows, so a partly correct table still earns marks.

Still finding trace tables tricky?

Book a free 30-minute online lesson and we will work through past-paper style trace tables together. Book a free demo →

Was this page helpful?