Courseiva

PCAP · domain

Exceptions and File I/O

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.

21 questions5 easy6 medium10 hard

Focused practice

Practice Exceptions and File I/O questions

Scored sessions drawing only from this domain — pick a length below.

What this domain covers

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.

Question index

All Exceptions and File I/O questions (21)

Click any question to see the full explanation, or start a practice session above.

1

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?

Hard
2

Refer to the exhibit. What is the output of the code?

Medium
3

Match each Python data structure to its mutability.

Medium
4

What is the output of the following code? try: exec('1/0') except: print('error') else: print('no error') finally: print('done')

Hard
5

Refer to the exhibit. A developer is writing a script to read this JSON configuration file. The script should write the logging configuration to a separate file called 'logging.conf'. Which file mode should be used to create the file if it doesn't exist, and overwrite it if it does?

Medium
6

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

Easy
7

A senior developer in a team argues that using try-except blocks is slower than checking conditions with if statements. They propose replacing all try blocks that handle file I/O errors with existence checks using os.path.exists before opening files. During a code review, you recall that Python's official documentation and best practices prefer EAFP (Easier to Ask for Forgiveness than Permission) over LBYL (Look Before You Leap) in many cases, especially in concurrent environments. The team's application is a multi-threaded web server that serves static files from a shared directory. Which is the strongest counterargument against the senior developer's proposal?

Hard
8

A developer writes a script to read a configuration file that may not exist. The script should handle the error gracefully and continue. Which approach is most Pythonic?

Easy
9

Consider the code fragment: f = open('data.txt', 'r') data = f.read() process_data(data) f.close() What is the primary risk if an exception occurs during process_data(data)?

Hard
10

In Python, if you have a try block followed by an except clause that catches all exceptions, which of the following is true about the else clause?

Easy
11

Refer to the exhibit. What is the effect of using 'from None' in the raise statement?

Hard
12

A developer is creating a custom exception hierarchy for a library. The base exception is `LibraryError`. Which definition ensures that subclasses can be caught using the parent exception, but also allows distinguishing between different error types?

Hard
13

A developer is building a logging system that writes logs to a file. The system should handle disk-full situations gracefully without crashing the main application. Which approach is appropriate?

Medium
14

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

Hard
15

Which THREE of the following statements about Python's 'with' statement are true? (Select exactly 3)

Hard
16

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

Medium
17

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?

Easy
18

Refer to the exhibit. A developer ran the script and saw the above traceback. The intended behavior was to load a JSON configuration file, and if the file is missing, create a default config. What is the most likely root cause of the second exception (NameError)?

Hard
19

Which TWO of the following file modes will create a new file if it doesn't exist? (Select exactly 2)

Easy
20

A developer is working on a data pipeline that processes files from untrusted sources. The pipeline should catch and log any exception, but also ensure that sensitive information from the exception (e.g., file paths) is not exposed to end users. Which approach balances security and debugging?

Hard
21

A developer is implementing a custom exception for invalid data. Which class should the custom exception inherit from?

Medium

Frequently asked questions

What does the Exceptions and File I/O domain cover on the PCAP exam?
Exceptions and File I/O questions test whether you can apply the concept in context, not just recognise a definition.
How many questions are in this domain?
This page lists all 21 Exceptions and File I/O questions in the PCAP question bank. The actual exam draws from this domain proportionally to its weighting in the official exam blueprint.
What is the best way to practise this domain?
Start with a short focused session (10 questions) to identify gaps, then work through explanations. Repeat with a longer session once the weak areas feel solid.
Can I practise only Exceptions and File I/O questions?
Yes — the session launcher on this page filters questions to this domain only. Choose any session length for inline explanations and scoring.
python-pcap PYTHON-PCAP exceptions file io Practice Questions