Cambridge AS Level Computer Science 9618 · 11.2 Constructs

Q40 · 9618/21/M/J/22 Q6

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

Q409618/21/M/J/22original Q6Structured · 9 marks
Question 6 from 9618/21/M/J/22

Worked solution

(a)

Count the values that end in 7 while looping from 100 to 200. A number ends in 7 when it leaves remainder 7 after dividing by 10.

DECLARE ThisInt, Count : INTEGER
Count ← 0
FOR ThisInt ← 100 TO 200
    IF ThisInt MOD 10 = 7 THEN
        OUTPUT ThisInt
        Count ← Count + 1
    ENDIF
NEXT ThisInt
OUTPUT Count

Declare the loop variable and the counter as INTEGER, and set the counter to 0 [1]

A loop from 100 to 200 with no step [1]

Test each value inside the loop (MOD 10 = 7) [1]

Output the chosen value and add 1 to the counter inside the loop [1]

Output the counter after the loop [1]

Another correct way: loop ThisInt from 107 TO 197 STEP 10 and output and count each value.

(b)

Use nested IF statements, one test for each case, and ELSE for OTHERWISE.

IF MySwitch = 1 THEN
    ThisChar ← 'a'
ELSE
    IF MySwitch = 2 THEN
        ThisChar ← 'y'
    ELSE
        IF MySwitch = 3 THEN
            ThisChar ← '7'
        ELSE
            ThisChar ← '*'
        ENDIF
    ENDIF
ENDIF

A test of MySwitch against 1, 2 or 3 [1]

All three comparisons with their matching assignments [1]

The default '*' given by the last ELSE (or set before the tests) [1]

Completely correct IF ... THEN ... ELSE ... ENDIF syntax [1]

KEY IDEAA CASE structure can always be rewritten as nested IF statements, with the final ELSE doing the job of OTHERWISE.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/21/M/J/22 question 6
VIDEO
▶ Whole paper

Video solution of the whole paper · by CS BY AQIB KHAN. 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.