Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4.
Find how many characters must be hidden (length minus 4). Build a string of that many asterisks, then add the last four characters of the card number.
FUNCTION Conceal(CardNumber : STRING) RETURNS STRING
DECLARE MaskedString : STRING
DECLARE Count, NumHidden : INTEGER
NumHidden ← LENGTH(CardNumber) - 4
MaskedString ← ""
FOR Count ← 1 TO NumHidden
MaskedString ← MaskedString & "*"
NEXT Count
MaskedString ← MaskedString & RIGHT(CardNumber, 4)
RETURN MaskedString
ENDFUNCTIONFunction heading with the string parameter, RETURNS STRING and ENDFUNCTION [1]
All local variables declared, including the loop counter [1]
Number of characters to hide worked out as LENGTH(CardNumber) - 4 [1]
A loop that runs the correct number of times [1]
An asterisk joined onto the string inside the loop [1]
Last four characters added with RIGHT(CardNumber, 4) and the masked string returned [1]
A non-loop answer also scores: use LEFT on a string of 20 asterisks with the number worked out, then join RIGHT(CardNumber, 4) and return it.
Give the bounds for 100 rows and 2 columns, with the element type STRING.
DECLARE CardNumber : ARRAY[1:100, 1:2] OF STRING
Correct dimensions [1:100, 1:2] [1]
All other parts of the statement correct (DECLARE, name, ARRAY, OF STRING) [1]
The procedure changes the array, so the changes must reach the original array. Write:
Pass it BYREF (by reference) in the procedure header [1]
▶ Whole paper
Video solution of the whole paper · by Papersdock. 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.