OCR GCSE Computer Science Computational thinking, algorithms and programming (02), June 2025: Question 4

6 marks · Medium difficulty · Algorithm / Pseudo-code

Complete an algorithm that reads data from an external text file and outputs non-zero values, and identify how casting is used within the algorithm.

Practise this question

Question

Question 4: An algorithm reads numbers from the external text file 'data.txt' and outputs each number if it is not 0. Part (a) asks to complete the algorithm using OCR Exam Reference Language statements from a provided box: data, for, if, myFile.close(), myFile.readLine(), input(), myFile.writeLine(), open, print, temp, txt, x. The skeleton code has 5 blanks to fill. Part (b) asks to identify how casting is used in this algorithm for 1 mark.
Question text

4 An algorithm is written to read numbers from the external text file data.txt

Each number is checked when it is read from the external text file. The number is output to the

user if it is not 0.

(a) Complete the algorithm using the OCR Exam Reference Language statements in the box.

Not all statements are used.

data for if

myFile.close() myFile.readLine() input()

myFile.writeLine() open print

temp txt x

myFile = … ("data.txt")

while NOT myFile.endOfFile()

temp = …

if int ( … ) != 0

… (temp)

endif

endwhile

[5]

(b) Identify how casting is used in this algorithm.

… [1]

Mark scheme

Show the mark scheme Mark scheme for Question 4: Part (a) awards 5 marks for filling the blanks with: 'open', 'myFile.readLine()', 'temp', 'print', and 'myFile.close()'. Part (b) awards 1 mark for stating: 'Convert data to an integer / int', with guidance stating not to allow a generic answer like 'change to different data type'.

Question Answer Mark Guidance

4 (a) myFile = open("data.txt") 5 Accept readLine for BP2 / close for BP5

Ignore spelling errors / case / minor differences as

while NOT myFile.endOfFile()

long as clear which option has been selected.

temp = myFile.readLine()

Accept MyFile.readLine() as 3rd missing

if int(temp) != 0 option (instead of temp) only if not used on line

above.

print(temp)

endif

Only allow one choice per gap. Mark incorrect if

endwhile multiple options chosen, e.g.

myFile.readLine(x)

myFile.close()

4 (b) • Convert data to an integer / int 1 Do not allow generic “change to different data type”

How to answer it

File Handling and Casting in Algorithms

📋 What this question tests

This question evaluates your ability to complete an algorithm using standard OCR Exam Reference Language syntax. It tests fundamental external file operations (opening, reading sequentially, and closing files), standard screen output, and the concept of data type casting (converting text to an integer).

Part (a) • 5 Marks

Completing the File Handling Algorithm

Selecting the correct operations from the word bank

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

✅ Mark Breakdown & Solutions

  • Gap 1: open — opens the specified file.
  • Gap 2: myFile.readLine() (or readLine ) — reads the next record/line into the variable.
  • Gap 3: temp — passes the variable into the int() casting function.
  • Gap 4: print — outputs the value to the screen.
  • Gap 5: myFile.close() (or close ) — safely closes the file stream outside the loop.

💡 Key Knowledge

  • Lifecycle of a file: You must always open a file before processing and close() it when processing is finished.
  • Reading line-by-line: readLine() reads the current line and advances the file pointer.
  • Sentinel condition: endOfFile() checks if the pointer has reached the end of the text file.

🧠 Exam Technique

  • One choice per gap: Only write a single statement per line. Writing combinations like myFile.readLine(x) scores 0.
  • Trace variable names: Notice that temp holds the retrieved line; therefore, temp must be passed into int(...) and printed.
  • Check brackets: Gaps with pre-printed brackets like ("data.txt") need the command name only ( open ), not another set of parentheses.

❌ Common Traps

  • Wrong method: Selecting myFile.writeLine() or input() instead of myFile.readLine() . The question asks to read from a file, not write to it or read from a keyboard!
  • Forgetting to close: Putting the close statement inside the while loop instead of at the end.
Examiner Guidance: Minor capitalisation and spelling mistakes are ignored as long as your intention is unambiguous.
Part (b) • 1 Mark

Identifying How Casting is Used

Understanding data type conversion in context

✅ Correct Answer

Any statement clearly stating conversion to an integer:

  • "It converts the data / string into an integer / int."
  • "Converts temp from text/string to an integer so it can be compared to 0."

❌ Common Misconception

  • Too vague: Stating "it changes to a different data type" scores 0 marks.
  • You must name the specific data type ( integer or int ) to be awarded the mark.

💡 Why was casting needed here?

All data read from an external text file is read as a String (text). To compare the value numerically with the integer literal 0 using != 0 , the program must cast the string value stored in temp to an int .

Examiner Guidance: Always link definition questions back to the code given: identify both the action (conversion) and the target data type (integer).

Topics

2.1 Algorithms · 2.2 Programming fundamentals · 2.1.2 Designing, creating and refining algorithms · 2.2.2 Data types · 2.2.3 Additional programming techniques

Question and mark scheme from the OCR GCSE Computer Science examination, Computational thinking, algorithms and programming (02), June 2025. QuestionVault is an independent revision resource; questions remain the copyright of the awarding body.