OCR A-level Computer Science practice questions

Trace tables, code and theory, with the reason behind every answer.

Repair one topic before your mock

Your board

A trace table, marked

OCR A-level Computer Science H446 · Component 02: Algorithms and programming

A program totals readings greater than 4. Array indexing starts at 0. The for loop includes both its first and last index. Record the trace at the end of each loop pass, after any addition.

01 array readings[5] = [3, 8, 2, 7, 4]
02 total = 0
03 for i = 0 to 4
04   if readings[i] > 4 then
05     total = total + readings[i]
06   endif
07 next i
08 print(total)
Complete the trace
ireadings[i]Condition true?total
0
1
2
3
4

Complete the trace table.

4 marks

State the value printed on line 08.

1 mark

The answer

Index 0 reads 3: 3 > 4 is false, so total remains 0. Index 1 reads 8: the condition is true, so total becomes 8. Index 2 reads 2: false, total stays 8. Index 3 reads 7: true, total becomes 15. Index 4 reads 4: false because the comparison is strictly greater than, so total stays 15.

State at the end of each pass
ireadingConditiontotal
03False0
18True8
22False8
37True15
44False15

15. Only 8 and 7 are added.

How it is marked

a. Trace 4 marks
One mark for all five reading values; one for all five condition results; one for the first three total values 0, 8, 8; one for the last two total values 15, 15. Repeated carried-forward totals after one arithmetic slip can receive method credit.
b. Output 1 mark
15, with no extra values.

See the full worked answer

Programming

Data representation

Boolean algebra and logic

Data structures and algorithms

Independent practice for OCR A-level Computer Science (H446), not endorsed by OCR.

Privacy · Terms