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.

PaperSL and HLHL onlyAll
Paper 251253

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.

Question 1 · CS-01040Paper 2 · 15 marks

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

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 / count

Java

Java
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;
}
  1. (a)

    State the data type of total and of count.

    2 marks

  2. (b)

    Identify three potential points of failure in this function/method.

    3 marks

  3. (c)

    Describe the role of a finally block in this function/method.

    2 marks

  4. (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

  5. (e)

    Explain why exception handling matters more for this program than for a program a person runs on a desktop computer.

    2 marks

Question 2 · CS-01211Paper 2 · HL only · 8 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).

Plain text
The cat sat
on the mat
the Cat ran
  1. (a)

    Construct a function/method count_unique(filename) (Python) or countUnique(filename) (Java) that returns the number of different words in the file. You may assume the file exists.

    6 marks

  2. (b)

    Outline why a set is a more suitable structure than a list for this task.

    2 marks

Question 3 · CS-01230Paper 2 · 5 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.

  1. (a)

    State the file mode that the program should use when it opens visits.txt to record a new visit.

    1 mark

  2. (b)

    A programmer opens the file in write mode instead, using open("visits.txt", "w") (Python) or new FileWriter("visits.txt") (Java). Explain the effect this has on the records already stored in the file.

    2 marks

  3. (c)

    Outline why the program should close visits.txt as soon as the new visit has been written.

    2 marks

Question 4 · CS-01231Paper 2 · 7 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:

Plain text
18.5
21.0
19.25
22.75
  1. (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 returns 20.375.

    You may assume the file exists and holds at least one reading.

    5 marks

  2. (b)

    Outline why each line read from the file must be converted before it is added to the total.

    2 marks

Question 5 · CS-01232Paper 2 · 7 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:

Plain text
Dune,21
Matilda,7
Holes,15
Emma,14
Kim,30

The following code processes the file.

Python

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

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);
  1. (a)

    Copy and complete the trace table for the code, using the file above.

    title (parts[0])

    days

    days > 14

    overdue

    output

    0

    4 marks

  2. (b)

    Explain the purpose of the call to readline() / readLine() at the end of the loop body.

    2 marks

  3. (c)

    State the value returned by readline() (Python) or readLine() (Java) when the end of the file has been reached.

    1 mark

Question 6 · CS-01233Paper 2 · 6 marks

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:

Plain text
H2201,Cardiology
H3075,Maternity
  1. (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 of checkins.txt, keeping every existing line. For example, log_checkin("H3076", "Oncology") adds the line H3076,Oncology.

    4 marks

  2. (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.

The notes on B2.5 File processing

More questions in Programming

  1. B2.1Programming fundamentals
  2. B2.2Data structures
  3. B2.3Programming constructs
  4. B2.4Programming algorithms
  5. B2.5File processingThis page

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.