Cambridge AS Level Computer Science 9618 · 11.3 Structured Programming

Q71 · 9618/22/M/J/22 Q7

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

Q719618/22/M/J/22original Q7Structured · 6 marks
Question 7 from 9618/22/M/J/22

Worked solution

(a)

Use RIGHT to cut off the front, then LEFT to keep only the characters needed.

FUNCTION MID(InString : STRING, Start, Num : INTEGER) RETURNS STRING
    DECLARE MidString : STRING
    DECLARE InStringLen : INTEGER
    InStringLen ← LENGTH(InString)
    MidString ← RIGHT(InString, InStringLen - Start + 1)
    MidString ← LEFT(MidString, Num)
    RETURN MidString
ENDFUNCTION

Function heading and ending with parameters and return type [1]

Correct use of one of the substring functions [1]

Correct use of both functions in the right order [1]

Return the substring [1]

Using LEFT first (length Start + Num - 1) and then RIGHT (length Num) also scores.

(b)

Any two checks:

Check that Start and Num are 1 or more (positive) [1]

Check that the string is long enough for the operation (Start + Num - 1 is not more than its length) [1]

KEY IDEACut away the start with RIGHT, then keep the wanted length with LEFT.
TOTAL 6 marks

Official mark scheme

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

Video solution of the whole paper · by Awab Aqib. 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.