PCAP · topic practice

Exceptions and File I/O practice questions

Practise Certified Associate Python Programmer PCAP Exceptions and File I/O practice questions — original exam-style scenarios with answer choices, explanations, and analysis of common mistakes.

Courseiva uses original exam-style practice questions designed for learning and revision. The goal is to understand the concepts, recognise exam patterns, and improve through explanations — not memorise copied exam dumps.

Reviewed byJohnson Ajibi· MSc IT Security
20 questionsDomain: Exceptions and File I/O

What the exam tests

What to know about Exceptions and File I/O

Exceptions and File I/O questions test whether you can apply the concept in context, not just recognise a definition.

How the topic appears in realistic exam-style scenarios.

Which detail in the question changes the correct answer.

How to eliminate plausible but wrong options.

How to connect the question back to the wider exam objective.

Watch out for

Common Exceptions and File I/O exam traps

  • Answering from memory before reading the full scenario.
  • Missing a constraint such as cost, availability, security, scope or command context.
  • Choosing a broad answer when the question asks for the most specific fix.
  • Ignoring why the wrong options are tempting.

Practice set

Exceptions and File I/O questions

20 questions · select your answer, then reveal the explanation

A developer writes a function that reads a configuration file and returns its contents as a string. The file might not exist. Which exception should be caught to handle a missing file?

Question 2mediummultiple choice
Study the full Python automation breakdown →

A Python script processes a log file line by line. If a line cannot be decoded due to an encoding error, the script should skip the line and continue. Which exception handling approach is best?

A developer implements a custom exception class `DataError` that inherits from `Exception`. Which method override is essential to ensure the exception message is properly displayed when caught?

Which of the following is the correct way to open a file for writing in text mode, ensuring that if the file already exists it will be overwritten?

A script uses `with open('data.bin', 'rb') as f:` to read binary data. Within the block, which method should be used to read exactly 4 bytes?

Consider the following code snippet:

try:

x = int(input()) y = 10 / x

print(y)

except ZeroDivisionError:

print('Division by zero')

except ValueError:

print('Invalid integer')

If the user enters '0', what is the output?

Which of the following statements about the `finally` block is true?

A programmer wants to catch both `FileNotFoundError` and `PermissionError` with a single except clause. Which tuple is correct?

Which TWO of the following are valid ways to raise an exception in Python?

Which THREE of the following are true about the `with` statement for file handling?

Which TWO of the following are built-in Python exceptions?

What is the output of the Python code after reading the config.txt file?

Exhibit

Refer to the exhibit.

# config.txt
[Settings]
host = localhost
port = 8080
debug = True

# Python code
import configparser

config = configparser.ConfigParser()
config.read('config.txt')
port = config.getint('Settings', 'port')
print(port)

What is the output of the code?

Exhibit

Refer to the exhibit.

# error.log
2025-03-15 10:23:45 ERROR: Division by zero in module calc.py line 42
2025-03-15 10:23:46 WARNING: Low disk space on /dev/sda1
2025-03-15 10:23:47 INFO: User login successful

# Python code
try:
    with open('error.log', 'r') as f:
        for line in f:
            if 'ERROR' in line:
                raise RuntimeError(line.strip())
except RuntimeError as e:
    print('Error found:', e)

You are a Python developer at a financial firm. Your team maintains a high-frequency trading system that reads real-time market data from a binary file 'market_feed.bin'. The file is updated every millisecond by an external process. Your script uses a `with` statement to open the file in binary read mode and reads exactly 1024 bytes each iteration in an infinite loop. Recently, the script has been crashing intermittently with an `OSError` with message '[Errno 9] Bad file descriptor'. The error occurs at random times, sometimes after minutes, sometimes after hours. The system runs on Linux. The script runs as a daemon. You suspect the issue is related to file descriptor exhaustion or the external process manipulating the file. What is the most likely cause and the best course of action?

Drag and drop the steps to read a CSV file using the csv module in Python into the correct order.

Drag steps to the numbered slots on the right, or tap a step then tap a slot.

Steps
Order
1Step 1
2Step 2
3Step 3
4Step 4
5Step 5

Drag and drop the steps to serialize a Python object to JSON using the json module into the correct order.

Drag steps to the numbered slots on the right, or tap a step then tap a slot.

Steps
Order
1Step 1
2Step 2
3Step 3
4Step 4
5Step 5

Match each Python data structure to its mutability.

Drag a concept onto its matching description — or click a concept then click the description.

Concepts
Matches

Mutable

Immutable

Mutable

Immutable

Mutable

Match each Python module to its purpose.

Drag a concept onto its matching description — or click a concept then click the description.

Concepts
Matches

Mathematical functions

Generate pseudo-random numbers

Manipulate dates and times

Work with JSON data

Interact with operating system

A developer writes a function that reads a file and processes its content. The function should handle the case where the file does not exist without catching other I/O errors. Which exception should be caught?

A developer is writing a script that processes user-uploaded CSV files. The script should attempt to read the file, and if a UnicodeDecodeError occurs, log a warning and skip the file. Which code snippet correctly achieves this without stopping the entire process?

Free account

Track your progress over time

Create a free account to save your results and see which topics improve across sessions.

Focused Exceptions and File I/O sessions

Start a Exceptions and File I/O only practice session

Every question in these sessions is drawn from the Exceptions and File I/O domain — nothing else.

Related practice questions

Related PCAP topic practice pages

Move into related areas when this topic feels solid.

Frequently asked questions

What does the PCAP exam test about Exceptions and File I/O?
Exceptions and File I/O questions test whether you can apply the concept in context, not just recognise a definition.
How should I use these practice questions?
Select your answer before revealing the explanation. Then read why each option is right or wrong — this active recall approach builds retention far faster than re-reading notes.
Can I practise just Exceptions and File I/O questions in a focused session?
Yes — the session launcher on this page draws every question from the Exceptions and File I/O domain. Use a 10-question session first to gauge your baseline, then move to 20 or 30 once the weak spots are clear.
Where can I practise other PCAP topics?
Use the topic links above to move to related areas, or go back to the PCAP question bank to see all topics.
Are these real exam questions or dumps?
These are original practice questions written to test the same concepts the PCAP exam covers. They are not copied from any real exam or dump site.