Practise File handling. 12 exam-style questions plus unlimited generated ones on this subtopic, at up to four difficulty levels, with full mark schemes and a progress tracker. Free, no account needed.
Written for this site in the style of OCR exam questions. They are not taken from real past papers.
Question 1Easy3 marks
(a) A program writes data to a file. Put these three steps into the correct order: close the file, write the data, open the file.[1]
(b) Give one reason why a program should close a file when it has finished using it.[1]
(c) Give one reason why a program would store data in a file rather than only in variables.[1]
Show the answer and mark scheme
(a)
open the file, write the data, close the file
(b)
so the file can be used by other programs / is not left locked
to make sure all the data is saved / written to the file
to free up the resources / memory used by the open file
(c)
data in variables is lost when the program ends, but data in a file is kept (so it can be used again next time the program runs)
Question 2Medium5 marks
The text file scores.txt contains one score on each line.
(a) Write an algorithm that reads every line of scores.txt and then outputs the number of lines in the file. You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[4]
(b) Explain why a condition-controlled loop is used rather than a count-controlled loop.[1]
Show the answer and mark scheme
(a)Answer:
f = open("scores.txt")
count = 0
while NOT f.endOfFile()
line = f.readLine()
count = count + 1
endwhile
f.close()
print(count)
opens scores.txt
loops until the end of the file is reached (e.g. while NOT f.endOfFile())
reads a line and adds 1 to a counter inside the loop (counter set to 0 before the loop)
closes the file and outputs the count after the loop
(b)
the number of lines in the file is not known in advance
Question 3Hard7 marks
The file results.txt contains test scores, one score on each line. The file contains at least one score.
(a) Write an algorithm that reads the scores from the file and outputs the mean score and the highest score. You must use either OCR Exam Reference Language or a high-level programming language that you have studied.[6]
(b) Explain why your algorithm would fail if the file was empty.[1]
Show the answer and mark scheme
(a)Answer:
f = open("results.txt")
total = 0
count = 0
highest = -1
while NOT f.endOfFile()
score = float(f.readLine())
total = total + score
count = count + 1
if score > highest then
highest = score
endif
endwhile
f.close()
print("Mean: " + str(total / count))
print("Highest: " + str(highest))
opens the file (and closes it after reading)
loops until the end of the file
reads each score and converts it to a number
keeps a running total and a count of the scores
keeps track of the highest score (starting from the first score or a value lower than any score, and updating it when a larger score is read)
after the loop, calculates the mean as total ÷ count and outputs the mean and the highest score
(b)
count would be 0, so calculating the mean would divide by zero (error)