Edexcel GCSE Computer Science (1CP2) · Problem solving with programming
Practise Input, output and file handling. 19 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.
Cover the answers and test yourself. The app has these as flashcards that come back just before you'd forget them.
State what happens to the data already stored in a file when the file is opened with the mode "w".
It is deleted (overwritten), so the file starts empty.
The text file birds.txt contains these four lines.
robin
wren
blackbird
starling
This program reads the file.
01 file = open("birds.txt", "r")
02 for line in file:
03 print(line.strip().upper())
04 file.close()
State the output of the program.
ROBIN
WREN
BLACKBIRD
STARLING
Identify the type of validation check carried out in line 02.
Length check
State the contents of visitors.txt after the program has run.
Mon
Tue
Wed
Identify the type of validation check carried out by lines 06 and 07.
Range check
Sample questions
Written for this site in the style of Edexcel exam questions. They are not taken from real past papers.
Question 1Easy4 marks
A program stores data in text files.
(a) Identify the line of code that opens the file log.txt so that new data is added to the end of the data already in the file.[1]
file = open("log.txt", "r")
file = open("log.txt", "w")
file = open("log.txt", "a")
file = open("log.txt", "read")
(b) State what happens to the data already stored in a file when the file is opened with the mode "w".[1]
(c) Give one reason why a program should close a file when it has finished using it.[1]
(d) Define the term 'CSV file'.[1]
Show the answer and mark scheme
(a)Answer:file = open("log.txt", "a")
(b)Answer: It is deleted (overwritten), so the file starts empty.
it is deleted / overwritten / lost (the file is emptied)
(c)Answer: To make sure that all the data has been written to (saved in) the file.
to make sure all data is written / saved to the file / to free the file so that other programs (or users) can use it / to free up resources
(d)Answer: A text file in which each line holds one record and the fields in a record are separated by commas.
a (text) file where each line is a record and the fields / values are separated by commas
Question 2Medium7 marks
A website uses this program when a new user chooses a username.
01 username = input("Choose a username: ")
02 while len(username) < 5 or len(username) > 12:
03 print("Username must be 5 to 12 characters")
04 username = input("Choose a username: ")
05 print("Welcome,", username)
(a) The user enters sam, then samantha_jones_2011, then samj99. State all of the output. Do not include the prompts.[3]
(b) Identify the type of validation check carried out in line 02.[1]
(c) State how many times the condition in line 02 is checked for these three inputs.[1]
(d) Usernames must not contain any spaces. Amend line 02 so that the program also rejects any username that contains a space.[2]
Show the answer and mark scheme
(a)Answer:
Username must be 5 to 12 characters
Username must be 5 to 12 characters
Welcome, samj99
Username must be 5 to 12 characters for sam
Username must be 5 to 12 characters again for samantha_jones_2011 (19 characters)
Welcome, samj99 as the last line
(b)Answer: Length check
length (check)
(c)Answer: 3
3
(d)Answer:while len(username) < 5 or len(username) > 12 or username.find(" ") != -1:
a condition that is True when the username contains a space, e.g. username.find(" ") != -1 (accept " " in username)
joined to the length conditions with or, so the loop repeats if any of the conditions is True
Question 3Hard7 marks
The file results.csv holds the name and test mark of each student in a class. Each line holds one record. The number of records in the file can change. At the moment the file contains these lines.
Aisha,62
Ben,48
Chloe,75
Dev,55
(a) Write a Python program that reads the file, calculates the mean mark and then outputs the name of every student whose mark is above the mean.[6]
(b) State the output that a correct program would produce for the four records shown.[1]
Show the answer and mark scheme
(a)Answer:
names = []
marks = []
file = open("results.csv", "r")
for line in file:
fields = line.strip().split(",")
names.append(fields[0])
marks.append(int(fields[1]))
file.close()
total = 0
for mark in marks:
total = total + mark
mean = total / len(marks)
for i in range(len(names)):
if marks[i] > mean:
print(names[i])
opens results.csv for reading (and closes it)
loops through every line in the file
removes the newline and splits each line at the comma
converts each mark to an integer (or real number)
stores the names and marks (e.g. in two lists or a two-dimensional list) so they can be used after the file has been read
calculates the mean as the total of the marks divided by the number of students
loops through the stored records again after the mean has been calculated
outputs only the names of the students whose mark is greater than the mean