Skip to main content
A-LevelComputer ScienceProgramming9618/21 · Oct/Nov 2024 · Q39 Marks

An algorithm will output the last three lines from a text file Result.txt The lines need to be output in the same order as they appear in the file. Assume: • Three variables LineX, LineY and LineZ will store the three lines. These are of type string and all three variables have been initialised to an empty string. • The file exists and contains at least three lines.

  1. (a)The algorithm to output the lines is expressed in eight steps. Complete the steps. 1. Open the file 2. Loop until 3. 4. Assign LineY to LineX 5. Assign LineZ to LineY 6. Assign ThisLine to LineZ 7. After the loop, 8. Output LineX, LineY, LineZ and store in ThisLine[4]
  2. (b)Explain the purpose of steps 4, 5 and 6 in the algorithm from part (a).[1]
  3. (c)The requirement changes, and the algorithm will now output three lines from the file, starting from a given line number. The modified algorithm will be implemented as a function which will: • be called with an integer parameter representing the given line number • output three lines, starting at the given line • return TRUE if the 3 lines are output, or FALSE if it was not possible to output the 3 lines. Describe the changes that need to be made to steps 2 to 8 of the algorithm given in part (a).[4]

Mark scheme

Marking points from the official mark scheme (Oct/Nov 2024). The mark or mark type is shown in brackets.

  1. [1] (a) 1. Open the file (Result.txt) in read mode
  2. [1] (a) 2. Loop until EOF(Result.txt) // EOF // end of file
  3. [1] (a) 3. Read a / the / next line (from the file) and store in ThisLine
  4. [1] (a) 4. Assign LineY to LineX
  5. [1] (a) 5. Assign LineZ to LineY
  6. [1] (a) 6. Assign ThisLine to LineZ
  7. [1] (a) 7. After the loop, close the file (Result.txt)
  8. [1] (a) 8. Output LineX, LineY, LineZ
  9. [1] (b) So that the last three lines of the file are output in the correct order
  10. [1] (b) (Ensuring) the lines are output in correct order
  11. [1] (b) Ordering the last three lines
  12. [1] (b) Ordering the lines so they are in the same order as (they occur) in the file
  13. [1] (c) Loop until given line number (-1)/ <parameter> (-1) (lines have been read)
  14. [1] (c) if end of file is reached then return FALSE
  15. [1] (c) Loop for three lines // Read three lines // Repeat of 4 and 5 for three lines
  16. [1] (c) Read a line and output it
  17. [1] (c) if end of file is reached then return FALSE
  18. [1] (c) After outputting the (required) lines return TRUE
  19. [1] (c) Ordering of lines no longer needed

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 →

About this question

Topic
Programming, A-Level Computer Science (syllabus 9618). Structured question, 9 marks.
Source
A-Level Computer Science, Oct/Nov 2024 Paper 2 Variant 1, question 3.

Questions and mark schemes are from past examination papers and remain the copyright of the exam board. How Oracle Prep builds its content

Related Computer Science questions

© 2026 Oracle Prep