Worked solution from the Data Types and Structures Paper 2 Problem-solving and Programming booklet, Part 3 of 4.
Fill each gap:
1: Open the file Result.txt in read mode [1]
2: Loop until EOF(Result.txt) (the end of the file) [1]
3: Read the next line from the file and store in ThisLine [1]
7: After the loop, close the file [1]
Write: steps 4, 5 and 6 keep the last three lines in the correct order, with each new line moved along (the same order as in the file) [1]
Two changes are needed. Either method scores:
Loop until the given line number minus 1 lines have been read, and return FALSE if the end of file is reached first [1]
Then loop three times, reading one line each time and outputting it [1]
Return FALSE if the end of file is reached while reading these lines [1]
After the three lines are output, return TRUE [1]
Other accepted points: the steps that move LineX, LineY and LineZ along are no longer needed; or one loop can read up to the given line number + 2 (or EOF), keeping steps 4 to 6 and returning FALSE if EOF is reached.
▶ Whole paper
Video solution of the whole paper · by Pseudocode Pro. 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.