Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 4 of 4.
The bottom of the stack holds the oldest value. A POP moves TopOfStack down one location each time.
| Answer | |
|---|---|
| The value that has been on the stack for the longest time. | 'H' |
| The memory location pointed to by TopOfStack if three POP operations are performed. | 206 |
Longest on the stack: 'H' (it is at the bottom, location 207) [1]
Location after three POPs: 206 (203 → 204 → 205 → 206) [1]
Work through the operations. The top starts at 202, and a POP moves it down to a higher address; a PUSH moves it up and overwrites that location.
POP → top at 203 ('Y'); POP → top at 204 ('X'); PUSH 'A' → 203 = 'A'; PUSH 'B' → 202 = 'B'; POP → top at 203; PUSH 'C' → 202 = 'C'; PUSH 'D' → 201 = 'D'.
| Memory location | Value | Pointer |
|---|---|---|
| 200 | ||
| 201 | 'D' | TopOfStack |
| 202 | 'C' | |
| 203 | 'A' | |
| 204 | 'X' | |
| 205 | 'Z' | |
| 206 | 'N' | |
| 207 | 'P' |
TopOfStack pointing to 'D' (location 201) [1]
'D' in location 201 [1]
'C' in 202 and 'A' in 203 [1]
'X', 'Z', 'N', 'P' unchanged in 204 to 207 [1]
▶ Whole paper
Video solution of the whole paper · by CS BY AQIB KHAN. 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.