Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 4 of 4.
All 6 items can be popped before the stack is empty. Write: 6 [1]
Follow each operation. Pop WWW into Data1, pop YYY into Data2, push AAA (to 504), push BBB (to 505), pop and discard BBB, pop AAA into Data2. TopOfStack ends at 503.
| Memory location | Value | Pointer |
|---|---|---|
| 506 | ||
| 505 | BBB | |
| 504 | AAA | |
| 503 | XXX | TopOfStack |
| 502 | ZZZ | |
| 501 | NNN | |
| 500 | PPP | BottomOfStack |
| Variable | Value |
|---|---|
| Data1 | WWW |
| Data2 | AAA |
Values BBB and AAA shown in 505 and 504 (they stay in memory but are no longer in the stack) [1]
Values XXX, ZZZ, NNN and PPP unchanged [1]
Both pointers shown and labelled (TopOfStack at 503, BottomOfStack at 500) [1]
Both variables correct: Data1 = WWW and Data2 = AAA [1]
The data can be recovered (restored) the next time the program is run, because it is saved permanently and is not lost when the program ends [1]
Write the five steps:
Step 1: open the text file in WRITE mode [1]
Step 2: check that there is a value on the stack [1]
Step 3: POP a value from the stack [1]
Step 4: write the value to the text file [1]
Step 5: repeat from step 2 (loop while the stack has items) [1]
Another way: read the value at TopOfStack, write it, then decrement TopOfStack, and repeat.
▶ Starts at Q3 (37:30)
Starts at Q3 (37:30) · by Zafar Ali Khan (ZAK). 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.