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

Show as plain text

Cambridge AS Level Computer Science 9618 · 10.4 Introduction to Abstract Data Types (ADT) Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 4 of 4. Q70 9618/21/M/J/22 original Q4 Structured · 6 marks Worked solution (a) 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] (b) 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] KEY IDEA A stack is last in, first out: the pointer moves on each PUSH and POP, and popped values are simply overwritten later. TOTAL 6 marks Official mark scheme VIDEO ▶ Whole paper Video solution of the whole paper · by CS BY AQIB KHAN. Tap the picture to open it on YouTube.