Cambridge AS Level Computer Science 9618 · 10.4 Introduction to Abstract Data Types (ADT)

Q75 · 9618/23/M/J/23 Q3

Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 4 of 4.

Q759618/23/M/J/23original Q3Structured · 11 marks
Question 3 from 9618/23/M/J/23

Worked solution

(a)(i)

All 6 items can be popped before the stack is empty. Write: 6 [1]

(a)(ii)

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 locationValuePointer
506
505BBB
504AAA
503XXXTopOfStack
502ZZZ
501NNN
500PPPBottomOfStack
VariableValue
Data1WWW
Data2AAA

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]

(b)(i)

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]

(b)(ii)

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.

KEY IDEAA stack is last in, first out: POP and PUSH move the top pointer, and popped values are not erased from memory.
TOTAL 11 marks

Official mark scheme

Mark scheme for 9618/23/M/J/23 question 3
VIDEO
▶ 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.