DP Computer Science · HL / SL · B2 Programming

B2.5 File processing

Get started
Notes Quiz
Free preview 2/15
  1. Question 1

    A student writes a Python program that records a new sensor reading to a file every minute throughout the day. Which file mode should be used each time the file is opened to ensure no previous readings are lost?
    No clue? Show me the answer
    Correct answerCorrect!Incorrect
    C"a" — append mode, to add each reading to the end

    Step-by-step walkthrough

    Choose a solution method

    Method #1Direct Approach

    Step 1: Identify the requirement

    The program must add a new reading each minute without losing any previously saved readings. This means each run of the write operation must preserve existing file content.

    Step 2: Apply knowledge of file modes

    Append mode ("a") opens the file and positions the write pointer at the end, so new data is added without disturbing anything already stored. If the file doesn't exist yet, it is created.

    Step 3: Rule out conflicting modes

    Write mode ("w") would erase all previous readings every minute — catastrophic for a logging scenario. Read mode ("r") does not allow writing at all.

    Step 4: Select the correct answer

    The correct mode is "a" (append) because it adds new data to the end of the file each time, preserving the full history of readings.

    Method #2Process of Elimination

    Step 1: Identify what is being asked

    The question asks which file mode preserves existing data while still allowing new data to be written every minute.

    Step 2: Eliminate "r" — read mode

    "r" (read mode) does not allow writing. Attempting to write in read mode would raise an error, so this option is incorrect.

    Step 3: Eliminate "w" — write mode

    "w" (write mode) silently deletes all existing content before writing. Every minute, all previous readings would be lost — this contradicts the requirement.

    Step 4: Eliminate "rw" — read-write mode

    "rw" is not a standard Python file mode. Python uses "r+" for read-write, and it is not the appropriate pattern for simple sequential logging.

    Step 5: Select the correct answer

    "a" (append mode) is the only option that adds new content to the end of the file without erasing existing data, making it the correct choice.

  2. Question 2

    In Python, what is the primary advantage of using `with open("file.txt", "r") as f:` compared to `f = open("file.txt", "r")`?
    No clue? Show me the answer
    Correct answerCorrect!Incorrect
    BThe `with` statement automatically closes the file when the block exits, even if an error occurs

    Step-by-step walkthrough

    Choose a solution method

    Method #1Direct Approach

    Step 1: Identify the concept being tested

    This question tests understanding of Python's context manager (with statement) and why it is the recommended way to open files.

    Step 2: Apply knowledge of the with statement

    Python's with statement implements a context manager protocol. When the indented block exits — whether normally or due to an exception — the file's __exit__ method is called, which automatically closes the file handle.

    Step 3: Why this matters

    Without with, if an exception occurs before f.close() is called manually, the file remains open. This can cause resource leaks and potential data loss, particularly in Java where buffered writes may not be flushed.

    Step 4: Select the correct answer

    The correct answer is that the with statement automatically closes the file when the block exits, even if an exception is thrown inside the block.

    Method #2Process of Elimination

    Step 1: Identify what is being asked

    The question asks for the primary advantage of the with statement over manually opening a file — focusing on resource management.

    Step 2: Eliminate the speed claim

    "reads the entire file faster" is incorrect. The with statement is a resource management tool — it does not change the speed of file I/O operations.

    Step 3: Eliminate simultaneous read/write

    "allows both reading and writing simultaneously" is incorrect. The mode (read or write) is determined by the mode string argument ("r", "w", etc.), not by the with statement.

    Step 4: Eliminate the error prevention claim

    "prevents FileNotFoundError" is incorrect. The with statement does not suppress exceptions. A FileNotFoundError will still be raised if the file doesn't exist — you still need try/except for that.

    Step 5: Select the correct answer

    The correct answer is automatic file closing — the with statement guarantees the file is closed when the block exits, even if an error occurs inside it.

Free preview

13 more questions in this topic

← Previous topicB2.4 Programming algorithms
Koncepts

Learn it properly. Then practise like it's the real paper.

Start free

Features

  • Lessons
  • Past papers
  • Library
  • Homework Help
  • Duels

More

  • For parents
  • Compare
  • Plans & pricing
  • DP for students

Legal

  • Privacy
  • Terms
  • Account deletion

© 2026 Koncepts (product of PrepAiro, Inc). All rights reserved.
DP, IB, EE and TOK are terms of the International Baccalaureate Organization.

Made for IB DP students.