- Subjects
- Computer Science
- Question bank
- B2.5 File processing
B2.5 File processing questions for IB Computer Science
53 questions on File processing, written by Augment in the style of IB exam papers. Six of them are below in full, to answer on paper.
- IB Computer Science
- Part of B2 Programming
- SL and HL
Questions by paper and level
SL and HL questions are for students at either level. HL only questions cover what only HL students study.
| Paper | SL and HL | HL only | All |
|---|---|---|---|
| Paper 2 | 51 | 2 | 53 |
Counted on
Questions to try
Written by Augment in the style of IB exams, not past papers. Answer them on paper, then check each one against its markscheme with a free account.
An unattended river-level sensor writes one reading per line to a text file. The function/method below returns the mean reading in a file.
Python
def average_reading(filename):
total = 0.0
count = 0
file = open(filename, "r")
for line in file:
total = total + float(line)
count = count + 1
file.close()
return total / countJava
public static double averageReading(String filename) throws IOException {
double total = 0.0;
int count = 0;
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line = reader.readLine();
while (line != null) {
total = total + Double.parseDouble(line);
count = count + 1;
line = reader.readLine();
}
reader.close();
return total / count;
}- (a)
State the data type of
totaland ofcount.2 marks
- (b)
Identify three potential points of failure in this function/method.
3 marks
- (c)
Describe the role of a
finallyblock in this function/method.2 marks
- (d)
Construct, in Python or Java, an improved version that:
returns −1 if the file cannot be opened
skips any line that is not a valid number and outputs a message
returns −1 if there are no valid readings
always closes the file once it is open.
6 marks
- (e)
Explain why exception handling matters more for this program than for a program a person runs on a desktop computer.
2 marks
A language-learning website measures the vocabulary of a student's essay as the number of different words it uses. Each essay is saved in a text file. Words are separated by single spaces, there is no punctuation, and the same word in different letter cases counts once.
For example, a file containing these three lines has 6 different words: the, cat, sat, on, mat and ran (the two later uses of the and the word Cat on the last line are repeats).
The cat sat
on the mat
the Cat ran- (a)
Construct a function/method
count_unique(filename)(Python) orcountUnique(filename)(Java) that returns the number of different words in the file. You may assume the file exists.6 marks
- (b)
Outline why a set is a more suitable structure than a list for this task.
2 marks
A climbing gym records every member who enters the building. Each visit is stored as one line of the text file visits.txt, and the program adds a new line each time a member scans their card at the door.
- (a)
State the file mode that the program should use when it opens
visits.txtto record a new visit.1 mark
- (b)
A programmer opens the file in write mode instead, using
open("visits.txt", "w")(Python) ornew FileWriter("visits.txt")(Java). Explain the effect this has on the records already stored in the file.2 marks
- (c)
Outline why the program should close
visits.txtas soon as the new visit has been written.2 marks
A school weather station saves one air temperature reading, in °C, on each line of a text file. For example, the file readings.txt holds:
18.5
21.0
19.25
22.75- (a)
Construct, in Python or Java, a function/method
average_temp(filename)/averageTemp(filename)that opens the named file, reads every reading and returns the mean temperature. For the file above it returns20.375.You may assume the file exists and holds at least one reading.
5 marks
- (b)
Outline why each line read from the file must be converted before it is added to the total.
2 marks
A school library stores each book on loan as a line of loans.txt in the form title,daysOnLoan. A book is overdue when it has been on loan for more than 14 days. The file holds:
Dune,21
Matilda,7
Holes,15
Emma,14
Kim,30The following code processes the file.
Python
f = open("loans.txt", "r")
overdue = 0
line = f.readline()
while line != "":
parts = line.strip().split(",")
days = int(parts[1])
if days > 14:
print(parts[0])
overdue = overdue + 1
line = f.readline()
f.close()
print(overdue)Java
BufferedReader reader = new BufferedReader(new FileReader("loans.txt"));
int overdue = 0;
String line = reader.readLine();
while (line != null) {
String[] parts = line.split(",");
int days = Integer.parseInt(parts[1]);
if (days > 14) {
System.out.println(parts[0]);
overdue = overdue + 1;
}
line = reader.readLine();
}
reader.close();
System.out.println(overdue);- (a)
Copy and complete the trace table for the code, using the file above.
title (
parts[0])daysdays > 14overdueoutput
0
4 marks
- (b)
Explain the purpose of the call to
readline()/readLine()at the end of the loop body.2 marks
- (c)
State the value returned by
readline()(Python) orreadLine()(Java) when the end of the file has been reached.1 mark
A hospital's reception desk records each patient who arrives in the text file checkins.txt, one line per check-in in the form patientID,ward. For example:
H2201,Cardiology
H3075,Maternity- (a)
Construct, in Python or Java, a function/method
log_checkin(patient_id, ward)/logCheckin(patientId, ward)that adds one new line for the given patient to the end ofcheckins.txt, keeping every existing line. For example,log_checkin("H3076", "Oncology")adds the lineH3076,Oncology.4 marks
- (b)
Explain why the file must be opened in append mode rather than write mode in this function.
2 marks
Practice all 53 in Augment
Free with an account. Every question has its markscheme, and Augment keeps the ones you got wrong so you can come back to them.
More questions in Programming
- B2.1Programming fundamentals
- B2.2Data structures
- B2.3Programming constructs
- B2.4Programming algorithms
- B2.5File processingThis page
More on File processing
- B2.5 File processing notesWhat File processing covers, explained, with its key terms.
- IB Computer Science question bankEvery Computer Science topic counted, and one question from each with its markscheme.
- Computer Science exam formatEach Computer Science paper, how long it is and its share of the grade.
Check your answers in Augment
A free account opens all 53 questions on File processing with their markschemes, and keeps a record of what you have marked.