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.
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
open. The file has to be opened before it can be read. The result is stored inmyFile, which is why every later statement starts withmyFile.myFile.readLine(). Inside the loop, read the next line intotemp. Notinput(), which reads the keyboard, and notwriteLine(), which goes the other way.temp. Theiftests the line that was just read, cast to an integer.print. The number is output to the user.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.)xordatafor gap 3. Neither has been given a value. The variable istemp.- 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 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()
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
- Question 1: Normal, boundary and invalid test data, and a 1 to 100 range check 9 marks
- Question 2(a), (b): Count the passes of a flowchart loop, and describe the kinds of iteration 8 marks
- Question 3(a): Show the steps of a merge sort on eight numbers 4 marks
- Question 5(a), (b): A logic circuit from a description, and the truth table for A AND B 5 marks
- Question 5(d): Validate a 4 character PIN that must not be 1234 or 4321 6 marks
Every OCR J277 question we have worked
Learn it step by step
- F7.1 Reading and writing files Files and databases
- F1.8 Data types and casting Programming basics
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.