A program processes data using a stack. The data is copied to a text file before the program ends. (a) The following diagram shows the current state of the stack. The operation of this stack may be summarised as follows: • The TopOfStack pointer points to the last item added to the stack. • The BottomOfStack pointer points to the first item on the stack. • The stack grows upwards when items are added. Stack | Memory location | Value | Pointer | |:---|:---:|:---:| | 506 | | | | 505 | WWW | ← TopOfStack | | 504 | YYY | | | 503 | XXX | | | 502 | ZZZ | | | 501 | NNN | | | 500 | PPP | ← BottomOfStack |
- (a)(i)An error will be generated if an attempt is made to POP a value when the stack is empty. State the maximum number of consecutive POP operations that could be performed on the stack shown above before an error is generated.[1]
- (a)(ii)The following operations are performed: 1. POP and store value in variable Data1 2. POP and store value in variable Data2 3. PUSH value AAA 4. PUSH value BBB 5. POP and discard value 6. POP and store value in variable Data2 Complete the diagram to show the state of the stack and the variables after the given operations have been performed. Stack | Memory location | Value | Pointer | |:---|:---:|:---:| | 506 | | | | 505 | | | | 504 | | | | 503 | | | | 502 | | | | 501 | | | | 500 | | | Variable | Value ---|--- Data1 | Data2 |[4]
- (b)(i)State an advantage of writing the data from the stack to a text file before the program ends.[1]
- (b)(ii)A module SaveStack() will write the data from the stack to a text file. Express an algorithm for SaveStack() as five steps that could be used to produce pseudocode. Write the five steps. Step 1 Step 2 Step 3 Step 4 Step 5[5]
This question uses a diagram from the original paper, which isn't shown here. Open the full paper from the past papers archive to see it.
Mark scheme
Marking points from the official mark scheme (May/June 2023). The mark or mark type is shown in brackets.
- [1] 6
- [4] MP1 Values 'BBB' and 'AAA'
- [4] MP2 Values 'XXX' to 'PPP' (unchanged)
- [4] MP3 Both pointers and labelled22
- [4] MP4 Values of both variables
- [1] So that the data may be recovered / restored (the next time the program is run)
- [1] // the data is permanently saved / data is not lost when the program terminates
- [5] MP1 Open the text file in WRITE mode
- [5] MP2 Check there is a value on the stack
- [5] MP3 POP value
- [5] MP4 Write value to the text file
- [5] MP5 Repeat from Step 2 // loop referencing the stack items
- [5] Alternative solution: Not using POP primitive
- [5] MP1 Open the text file in WRITE mode
- [5] MP2 Check there is a value on the stack
- [5] MP3 Read value from Tos location
- [5] MP4 Write the value to the text file - Must some attempt at 'the value' NOT 'all the values'
- [5] MP5 Decrement ToS
- [5] MP6 Repeat from step 2 // loop referencing the stack items
Write your answer and get it marked
On Oracle Prep you can write or photograph your own answer and have it marked by AI against this mark scheme, with feedback on what you missed.
Sign up free →