OCR GCSE Computer Science June 2025 Paper 2, Question 4: reading from a text file

OCR J277/02 June 2025, Question 4: complete an algorithm that opens data.txt, reads each line until the end of the file, casts it to an integer and prints it if it is not 0. Each gap explained, with the algorithm to run on a real file.

Past paper questionOCR J277/02June 2025 Paper 26 marksComplete the algorithm

Question 4 of the OCR GCSE Computer Science Paper 2 sat on 20 May 2025 (J277/02) gives you a file reading algorithm with five gaps, and a box of statements to fill them from (5 marks). Part (b) asks how casting is used (1 mark). File handling has the same shape every time: open, loop to the end of the file, read a line, close.

We do not copy the exam paper here. Open it beside this page: OCR June 2025 J277/02 question paper (PDF). When you have finished, check the mark scheme too.

The question in short

Numbers are stored in a text file called data.txt, one on each line. The algorithm reads each number, and outputs it if it is not 0.

The finished algorithm

myFile = open("data.txt")
while NOT myFile.endOfFile()
    temp = myFile.readLine()
    if int(temp) != 0
        print(temp)
    endif
endwhile
myFile.close()

Work it through

  1. open. The file has to be opened before it can be read. The result is stored in myFile, which is why every later statement starts with myFile.
  2. myFile.readLine(). Inside the loop, read the next line into temp. Not input(), which reads the keyboard, and not writeLine(), which goes the other way.
  3. temp. The if tests the line that was just read, cast to an integer.
  4. print. The number is output to the user.
  5. myFile.close(). After the loop, close the file.

Part (b): how is casting used?

The line read from the file is a string. int(temp) converts it to an integer, so that it can be compared with the number 0.

The answer has to be about this algorithm. "Changing one data type to another" is the general definition, and it is refused here.

Where the marks are lost

  • input() for gap 2. The data comes from the file, not the user.
  • myFile.readLine() again in gap 3. That would read a second line and test that, skipping every other number. (The mark scheme only allows it if you did not use it in the gap above.)
  • x or data for gap 3. Neither has been given a value. The variable is temp.
  • Closing the file inside the loop. It would be closed after the first number. The close is lined up with myFile = open, outside the loop.
  • Two options in one gap. Choose one. myFile.readLine(x) is marked wrong.

Run it

This program writes a small data.txt first, so that there is something to read, and then runs the algorithm from the question. In Python a for loop over the file does the job of the while NOT endOfFile loop. The robot drives each number it prints, in centimetres.

The file holds 12, 0, 7, 0 and 5. It prints 12, 7 and 5: the zeros are skipped.
The program
from bugbot import *
connect()

# make a file to read
myFile = open("data.txt", "w")
myFile.write("12\n0\n7\n0\n5\n")
myFile.close()

myFile = open("data.txt", "r")
for line in myFile:
    temp = line.strip()
    if int(temp) != 0:
        print(temp)
        forward(60, distance=int(temp))
myFile.close()
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

Questions

What are the answers to OCR J277 June 2025 Paper 2 Question 4(a)?

The gaps are open, myFile.readLine(), temp, print and myFile.close().

How do you read a file in OCR Exam Reference Language?

Open it with myFile = open("data.txt"). Loop with while NOT myFile.endOfFile(), reading each line with myFile.readLine(). Close it afterwards with myFile.close().

Why does data read from a text file need casting?

Everything in a text file is read as a string, even if it looks like a number. To do arithmetic or compare it with a number, it has to be cast with int() or float().

More from this paper

Every OCR J277 question we have worked

Learn it step by step

  1. F7.1 Reading and writing files Files and databases
  2. F1.8 Data types and casting Programming basics
Open the lessons

This is our own explanation of a published exam question. It is not written or endorsed by OCR, and the question paper and mark scheme remain OCR's copyright. Read them on OCR's site with the links on this page.