Cambridge AS Level Computer Science 9618 · 10.3 Files

Q61 · 9618/21/O/N/24 Q3

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

Q619618/21/O/N/24original Q3Structured · 9 marks
Question 3 from 9618/21/O/N/24

Worked solution

(a)

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]

(b)

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]

(c)

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.

KEY IDEATo keep the last few lines of a file, shift each line along as a new one is read, then use the stored lines after the loop.
TOTAL 9 marks

Official mark scheme

Mark scheme for 9618/21/O/N/24 question 3
VIDEO
▶ 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.