Oxford Cambridge and RSA
GCSE (9-1) Computer Science
Algorithms & Programming
J277/02
Topic: 2.2 Programming Fundamentals (File Handling)
Time allowed: 45 minutes
Centre number Candidate number
First name Last name
INSTRUCTIONS INFORMATION
Turn over
Section A: Core Concepts & "Ghost" Theory
1
A program is written to read names from a text file called class_list.txt. Fill in the missing OCR Exam Reference Language keywords to complete the code.
myFile = open("class_list.txt") while NOT myFile.___________(): studentName = myFile.___________() print(studentName) endwhile myFile.___________()
[3]
2
When a program opens a text file to read data, the data is pulled into the program. Explain why it is essential to run the close() command once the program has finished with the file.
[2]
3
The file scores.txt contains the following single line of text:
100

Consider the following code snippet:
file = open("scores.txt") score = file.readLine() file.close() total = score + score print(total)
State the output of this program and explain why this output occurs.
[2]
Section B: Debugging & Tracing
4
A programmer wants to read a list of product prices from prices.txt, add 20% VAT to them, and print the new price.
The file prices.txt contains: 10, 50, 20.
01 file = open("prices.txt") 02 while NOT file.endOfFile(): 03 price = file.readLine() 04 newPrice = price * 1.2 05 print(newPrice) 06 endwhile 07 file.close()
(a) Identify the line number and correct the Syntax Error.
[1]

(b) Identify the line number and correct the Logic Error.
[2]
Turn over
5
A file data.txt contains the following numbers on separate lines: 5, 2, 8.
Trace the execution of the following algorithm.
x = 0 f = open("data.txt") while NOT f.endOfFile(): val = int(f.readLine()) if val > 4 then x = x + val else x = x - val endif endwhile f.close() print(x)
[4]
val val > 4 x Output
0
Section C: Coding & Application
6
A teacher has a file called maths_scores.txt. Each line contains a student's score out of 100.
Write a program in Python or OCR Exam Reference Language that:
  • Creates/Opens a new file called pass_list.txt for writing.
  • Reads every score from maths_scores.txt.
  • If the score is 50 or higher, writes the score to the new pass_list.txt file.
  • Ensures both files are closed safely.
[6]
7
The following code allows a user to log a diary entry.
entry = input("Enter your diary entry: ") file = open("diary.txt") file.writeLine(entry) file.close()
(a) Explain why the previous entries disappear.
[1]

(b) Write the single line of code that needs changing to fix this issue.
[1]
8
Write a defensive check (using pseudocode) that could be added before attempting to open maths_scores.txt in Question 6, to prevent the program from crashing if the file is missing.
[2]
END OF QUESTION PAPER