Skip to main content
SingaporeComputer ScienceSyllabus dot point

How do we check an algorithm works using a trace table and well-chosen test data?

Use a trace table to follow variable values through an algorithm, and choose normal, boundary and invalid test data

A focused answer to the O-Level Computing point on tracing and testing. Building a trace table to track variables through an algorithm, and choosing normal, boundary and invalid test data to find errors.

Generated by Claude Opus 4.87 min answer

Reviewed by: AI editorial process; not yet individually human-reviewed

Have a quick question? Jump to the Q&A page

Jump to a section
  1. What this dot point is asking
  2. The answer
  3. Examples in context
  4. Try this

What this dot point is asking

SEAB wants you to use a trace table to follow the values of variables as an algorithm runs, and to choose good test data: normal, boundary and invalid. The central idea is that tracing lets you check an algorithm by hand, step by step, while well-chosen test data exposes the errors that hide at the edges and outside the expected range.

The answer

What a trace table is

A trace table is a table with one column per variable (plus any output). You work through the algorithm one line at a time, writing down the new value whenever a variable changes. It lets you see exactly what the algorithm does, which is the surest way to find a logic error.

line / step   |  a  |  b  |  output

How to fill a trace table

  1. Make a column for each variable and for output.
  2. Start at the first line and work down, in order.
  3. Each time a variable changes, write its new value in a new row.
  4. For a loop, go round as many times as the loop runs, writing each pass.

Worked trace

Trace this for input 77:

x = 5
INPUT y        (y = 7)
x = x + y      (x = 12)
OUTPUT x
x y output
5
5 7
12 7 12

Choosing test data

Testing checks that a program behaves correctly. Use three kinds of data:

  • Normal (valid) data: typical values the program should accept, such as a mark of 5757.
  • Boundary data: values right at the edge of the allowed range, such as 00 and 100100, and just outside, such as −1-1 and 101101.
  • Invalid (erroneous) data: values the program should reject, such as 150150 or a letter where a number is expected.

Why boundaries matter most

Many bugs hide at the edges of a range, where a programmer might write > instead of >=. Testing the exact limits catches these off-by-one errors that normal data would slip past.

Examples in context

Example 1. Debugging a loop that runs one time too many. A trace table on a loop that should print 11 to 1010 but prints to 1111 instantly shows the condition uses <= where < was needed. Following the values by hand pinpoints the off-by-one error that staring at the code missed.

Example 2. Testing a login age check. A site requires users to be at least 1313. Boundary tests of 1212, 1313 and 1414 confirm that exactly 1313 is allowed and 1212 is not, catching a > versus >= mistake that normal ages like 2525 would never reveal.

Try this

Q1. State what each column of a trace table represents. [2 marks]

  • Cue. Each column holds the values of one variable (or the output) as the algorithm runs.

Q2. Give an example of boundary test data for a program that accepts ages 00 to 120120. [2 marks]

  • Cue. A value at the edge such as 00 or 120120, and just outside such as −1-1 or 121121.

Q3. Explain why invalid test data should be used. [2 marks]

  • Cue. To check the program correctly rejects values it should not accept, rather than crashing or storing bad data.

Exam-style practice questions

Practice questions written in the style of SEAB exam questions on this dot point, with worked answer explainers. The year tag is the paper they imitate, not the source.

Original5 marksTrace this algorithm with a trace table, for the input list [4,9,2][4, 9, 2], showing the value of total and i at each step. ``` total = 0 FOR i = 1 TO 3 INPUT num total = total + num NEXT i OUTPUT total ```
Show worked answer →

Set up columns for i, num, total. Start with total = 0, then process each input.

i    num   total
-    -     0
1    4     4
2    9     13
3    2     15

The loop runs three times, adding 44, then 99, then 22 to the running total. The output is the final total, which is 1515.

Markers reward a column per variable, total starting at 00, each row updating total correctly, and the output 1515.

Original5 marksA program accepts an exam mark from 00 to 100100. (a) Give one example each of normal, boundary and invalid test data for this program. (b) Explain why boundary testing is important.
Show worked answer →

(a) Examples:

  • Normal data: a typical valid value such as 5757, well inside the allowed range.
  • Boundary data: a value at the edge of the range, such as 00 or 100100 (and just outside, such as −1-1 or 101101).
  • Invalid data: a value the program should reject, such as 150150 or a letter like 'x'.

(b) Boundary testing is important because errors most often occur at the edges of a range, where a programmer may use the wrong comparison (for example > instead of >=). Testing the exact limits checks that the boundaries are handled correctly.

Markers reward one valid normal value, a value at the limit for boundary, an out-of-range value for invalid, and the point that errors cluster at boundaries.

Related dot points