Cambridge O Level Computer Science 2210 · 8.1 Programming concepts

Q36 · 2210/21/M/J/25 Q9

Worked solution from the Programming Paper 2 Algorithms, Programming and Logic booklet, Part 1 of 3.

Q362210/21/M/J/25original Q9Structured · 8 marks
Question 9 from 2210/21/M/J/25

Worked solution

(a)

Declare each variable with its data type. A holds a whole number and B holds text:

DECLARE A : INTEGER
DECLARE B : STRING

DECLARE A : INTEGER [1]

DECLARE B : STRING [1]

(b)

Define the function first. It needs a parameter X and it returns text. Use MOD to test for odd or even: an even number has remainder 0 when divided by 2.

FUNCTION Odds(X : INTEGER) RETURNS STRING
    IF MOD(X, 2) = 0
        THEN
            RETURN "Even"
        ELSE
            RETURN "Odd"
    ENDIF
ENDFUNCTION

B ← Odds(A)

Uses FUNCTION, ENDFUNCTION and RETURNS STRING [1]

Function is named Odds [1]

Parameter defined correctly, for example X : INTEGER [1]

Odd or even tested using MOD (or DIV or ROUND) on the parameter [1]

Returns "Odd" and "Even" correctly [1]

Function call stores the result in B: B ← Odds(A) [1]

KEY IDEAA function is defined with FUNCTION, a parameter and RETURNS, and it is called by putting the call on the right of an assignment.
TOTAL 8 marks

Official mark scheme

Mark scheme for 2210/21/M/J/25 question 9
VIDEO
▶ Topic lesson

A lesson on this topic · by Mr Bulmer's Learning Zone. 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.