9618/21/M/J/21 question 4: worked solution (May/June 2021, paper 21)

Show as plain text

Cambridge AS Level Computer Science 9618 · 12.3 Program Testing and Maintenance Worked solution from the Software Development Paper 2 Problem-solving and Programming booklet, Part 3 of 3. Q33 9618/21/M/J/21 original Q4 Structured · 10 marks Worked solution (a) Dry run the function one loop at a time. Flag is TRUE at the start, so the first character is made upper case. Flag becomes FALSE after a letter and TRUE again after a space. Name Flag Index NewName ThisChar "∇in∇a∇∇Cup" TRUE 1 "" '∇' "∇" 2 'i' FALSE "∇I" 3 'n' "∇In" 4 '∇' TRUE "∇In∇" 5 'a' FALSE "∇In∇A" 6 '∇' TRUE "∇In∇A∇" 7 '∇' "∇In∇A∇∇" 8 'C' FALSE "∇In∇A∇∇C" 9 'u' "∇In∇A∇∇Cu" 10 'p' "∇In∇A∇∇Cup" FALSE 11 "∇In∇A∇∇Cup" 'p' Flag column correct [1] Index column correct [1] NewName column correct [1] ThisChar column correct [1] Final row correct, including Index 11 [1] (b) The loop runs once for every character in Name, so the number of repeats is known before it starts. Count-controlled loop (a FOR … NEXT loop) [1] Justification: the number of iterations is known (it is LENGTH(Name)) [1] (c)(i) Make the character lower case when it is added after the first letter. Change line 24 (or line 31): ThisChar ← LCASE(MID(Name, Index, 1)) Modified line that uses LCASE on the character (either line 24 or line 31 version scores) [1] Accepted alternative: NewName ← NewName & LCASE(ThisChar) on line 31. (c)(ii) Line 26 adds the character to NewName when Flag is TRUE, even when it is another space. Move it inside the IF ThisChar <> SPACECHAR block, so extra spaces met while Flag is TRUE are skipped. Line number: 26 [1] New position: move it to after line 27 / after line 28 (inside the IF ThisChar <> SPACECHAR block) [1] KEY IDEA In a trace table update one row per loop pass and only write the values that change; a loop with a known number of repeats should be a FOR loop. TOTAL 10 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by Papersdock. Tap the picture to open it on YouTube.