Programming Constructs and Operators: Question 4
Syllabus 8.1
A banking app displays a customer's reference code on screen with only the last two characters visible; every character before that is replaced with an asterisk. This pseudocode algorithm produces that masked display, one character at a time, using a post-condition loop.
01 DECLARE Code : STRING
02 DECLARE Index : INTEGER
03 Code ← "48213"
04 Index ← 1
05 REPEAT
06 IF Index <= LENGTH(Code) - 2 THEN
07 OUTPUT "*"
08 ELSE
09 OUTPUT SUBSTRING(Code, Index, 1)
10 ENDIF
11 Index ← Index + 1
12 UNTIL Index > LENGTH(Code)
(a) State the value of LENGTH(Code) - 2 for Code = "48213", and state the smallest value
of Index for which the ELSE branch (line 8–9) is taken. [2]
(b) Complete a trace table showing the value of Index and the character output on each pass
of the loop, for Code = "48213". Hence state the complete sequence of characters displayed
on screen. [4]
(c) A different reference code, Code = "9", is used instead. State what is displayed on
screen when the algorithm executes with this value, showing your working. [2]
Show worked solution Hide worked solution
Worked solution
Part (a): Working out the masking boundary
LENGTH(Code) for Code = "48213" is 5 (5 characters). So:
LENGTH(Code) - 2 = 5 - 2 = 3
The IF condition on line 6 is Index <= 3. This is true for Index = 1, 2, 3 (so those
positions are masked with *), and becomes false for the first time when Index = 4. This
is the smallest value of Index for which the ELSE branch runs and the real character is
shown instead. [2 marks]: [1] for LENGTH(Code) - 2 = 3, [1] for identifying
Index = 4 as the first ELSE case.
Part (b): Tracing the REPEAT…UNTIL loop
Code = "48213" has characters 4 (position 1), 8 (position 2), 2 (position 3), 1
(position 4), 3 (position 5). The loop body always runs at least once (it is a post-condition
loop), and stops only once Index > LENGTH(Code) becomes true, i.e. once Index > 5.
| Pass | Index | Index <= 3 ? | Statement executed | Output |
|---|---|---|---|---|
| 1 | 1 | true | line 7: OUTPUT "*" | * |
| 2 | 2 | true | line 7: OUTPUT "*" | * |
| 3 | 3 | true | line 7: OUTPUT "*" | * |
| 4 | 4 | false | line 9: OUTPUT SUBSTRING(Code,4,1) | 1 |
| 5 | 5 | false | line 9: OUTPUT SUBSTRING(Code,5,1) | 3 |
After pass 5, Index becomes 6, and UNTIL Index > LENGTH(Code) checks 6 > 5, which is true,
so the loop stops. The five characters output in sequence, *, *, *, 1, 3, appear on
screen one after another as:
***13
[4 marks]: [1] for correctly identifying the three masked passes (Index = 1, 2, 3),
[1] for correctly extracting 1 at Index = 4 and 3 at Index = 5 with SUBSTRING,
[1] for a fully correct trace table, [1] for the correct final displayed sequence
***13.
Part (c): The edge case Code = “9”
LENGTH("9") = 1, so:
LENGTH(Code) - 2 = 1 - 2 = -1
On the very first (and only) pass, Index = 1. The condition Index <= LENGTH(Code) - 2
becomes 1 <= -1, which is false, since -1 is a negative number and 1 is not less than
or equal to it. So the ELSE branch runs immediately: OUTPUT SUBSTRING("9", 1, 1), which
outputs 9. Index becomes 2, and UNTIL 2 > 1 is true, so the loop stops after just one
pass.
The screen displays the single character in full, with no masking at all:
9
This makes sense: with only 1 character in Code, there aren’t even 2 characters left to keep
visible, let alone any earlier ones to hide. [2 marks]: [1] for correctly evaluating
LENGTH(Code) - 2 as negative, [1] for the correct final output 9.
Final answers
- (a)
LENGTH(Code) - 2 = 3; theELSEbranch first runs atIndex = 4. - (b) Trace as shown above, the screen displays
***13. - (c) The screen displays
9(no masking occurs).