Trace tables
Computer Science · Programming fundamentals
Question 1 of 10
0The algorithm below is traced with a trace table. Each row of the trace table records the values of the variables at the end of one pass of the for loop. total ← 2 for i ← 2 to 6 total ← total + i next i How many rows does the trace table have (one row for each pass of the loop)?
Decimals (0.75 or 0,75), fractions (3/4) and mixed numbers (1 1/2) are accepted. Do not use thousands separators.
How to type your answer
- Fractions: 3/4. Mixed numbers: 1 1/2.
- Decimals with a point or a comma: 0.75 or 0,75.
- Negative numbers start with a minus: -3/4.
- Percentages need the % sign: 75%.
Stuck? Read the explanation
Unlocks at a streak of 5 correct answers in a row.
Embed this practice on your site or class page
Paste this HTML into your page. Students practise right there; it is free and needs no account.
Trace tables explained
A trace table records the value of each variable at each step of an algorithm. It is the standard way to check by hand what a piece of code does, and exam questions often ask you to complete one. Each column is a variable, each row is a step, and you write a new row for each step or pass.
Work through the code line by line. For an assignment, evaluate the right-hand side using the current values, then record the new value. For a loop, keep one row for each pass and check the condition before every pass. When the condition fails, stop and record the final output in its own column.
Common mistakes include using a variable before it has been updated, forgetting that a WHILE condition is checked before the loop body runs, and forgetting to check the condition one last time to end the loop.
Worked example
Start with x = 3 and y = 0. The algorithm repeats while x > 0: it adds x to y, then subtracts 1 from x.
- Start: x = 3, y = 0.
- Pass 1: y = 0 + 3 = 3, then x = 3 - 1 = 2.
- Pass 2: y = 3 + 2 = 5, then x = 2 - 1 = 1.
- Pass 3: y = 5 + 1 = 6, then x = 1 - 1 = 0.
- Check: x > 0 is now false, so the loop ends.
The output is y = 6.
What you will practise
New numbers every time you practise. Levels: Easy, Medium, Hard. Each answer is followed by a worked explanation.
Example questions
Easy
The algorithm below is traced with a trace table. Each row of the trace table records the values of the variables at the end of one pass of the for loop.
points ← 0 for i ← 2 to 14 step 3 points ← points + i next i
What is the final value of points when the algorithm finishes?
Medium
The algorithm below is traced with a trace table. Each row of the trace table records the values of the variables at the end of one pass of the for loop.
sum ← 5 for i ← 1 to 6 if i MOD 3 = 0 then sum ← sum + i else sum ← sum - 1 endif next i
What is the value of sum in row 2 of the trace table (the 2nd pass of the loop)?
Hard
The algorithm below is traced with a trace table. Each row of the trace table records the values of the variables at the end of one pass of the while loop.
n ← 6 steps ← 0 while n ≠ 1 if n MOD 2 = 0 then n ← n DIV 2 else n ← 3 * n + 1 endif steps ← steps + 1 endwhile
What is the value of n in row 5 of the trace table (the 5th pass of the loop)?