Cambridge AS Level Computer Science 9618 · 11.2 Constructs

Q64 · 9618/23/O/N/25 Q5

Worked solution from the Programming Paper 2 Problem-solving and Programming booklet, Part 2 of 3.

Q649618/23/O/N/25original Q5Structured · 8 marks
Question 5 from 9618/23/O/N/25

Worked solution

(a)

Run the procedure one line at a time. Index goes 1, 2, 3, 4, 7, 4, 1, 3, 4. At CaseVar 5 the OTHERWISE sets Count to 20. The loop ends when Count reaches 22.

IndexCaseVarCountNum[1]Num[2]Num[3]Num[4]
101253
1
212
3
5
20
4
3
7196
4
6
20
1
2
3223
4

First three rows correct: starting values, CaseVar 1, then Index 2, Count 1 and Num[1] = 2 [1]

Index column correct from Index 3 downwards [1]

CaseVar column correct from 5 downwards [1]

Count column correct from 20 downwards [1]

Num[4] = 6 when Index is 7, and Num[1] = 3 when Index is 3 the second time [1]

(b)(i)

Clauses for CaseVar 1 and 2. [1]

(b)(ii)

Both clauses add Index to the element. The only difference is the step, which equals CaseVar, so use CaseVar instead of the numbers:

1 TO 2 : Num[Index] ← Num[Index] + Index
         Index ← Index + CaseVar
         Count ← Count + CaseVar

CaseVar used instead of the fixed numbers 1 and 2 [1]

Whole clause correct [1]

KEY IDEATrace one line at a time and write a value only when it changes; clauses that differ only by a number can be merged using the variable.
TOTAL 8 marks

Official mark scheme

Mark scheme for 9618/23/O/N/25 question 5
VIDEO
▶ Topic lesson

A lesson on this topic · by ZAINEMATICS. Tap the picture to open it on YouTube.

Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.