9618/22/O/N/24 question 2: worked solution (October/November 2024, paper 22)

Show as plain text

Cambridge AS Level Computer Science 9618 · 10.2 Arrays Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 2 of 4. Q32 9618/22/O/N/24 original Q2 Structured · 9 marks Worked solution (a) 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 ENDFUNCTION Function 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. (b)(i) 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] (b)(ii) The procedure changes the array, so the changes must reach the original array. Write: Pass it BYREF (by reference) in the procedure header [1] KEY IDEA Build the masked string with a loop of asterisks plus RIGHT(...,4), and pass arrays BYREF when a procedure must change them. TOTAL 9 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Papersdock. Tap the picture to open it on YouTube.