Practice PCAP Exceptions and File I/O questions with full explanations on every answer.
Start practicing
Exceptions and File I/O — choose a session length
Free · No account required
Click any question to see the full explanation and answer options, or start a focused practice session above.
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?
2Which of the following statements about the `finally` block is true?
3What is the output of the Python code after reading the config.txt file?
4Drag and drop the steps to serialize a Python object to JSON using the json module into the correct order.
5Match each Python data structure to its mutability.
6A 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?
7A 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?
8A 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?
9A 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?
10Refer 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)?
11Refer 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?
12What is the output of the following code? try: exec('1/0') except: print('error') else: print('no error') finally: print('done')
13Which TWO of the following file modes will create a new file if it doesn't exist? (Select exactly 2)
14Which THREE of the following statements about Python's 'with' statement are true? (Select exactly 3)
15In 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?
16Consider 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)?
17A 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?
18A developer is implementing a custom exception for invalid data. Which class should the custom exception inherit from?
19A 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?
20Refer to the exhibit. What is the output of the code?
21Refer to the exhibit. What is the effect of using 'from None' in the raise statement?
Deep-dive questions
The most-searched questions in this domain — detailed explanations, worked examples, full answer breakdowns.
The Exceptions and File I/O domain covers the key concepts tested in this area of the PCAP exam blueprint published by Python Institute. Courseiva provides free domain-focused practice, mock exams, missed-question review, and readiness tracking across all PCAP domains — no account required.
The Courseiva PCAP question bank contains 21 questions in the Exceptions and File I/O domain, covering the 36% of the exam attributed to this domain in the official Python Institute blueprint. Click any question to see the full explanation and answer breakdown.
Start with a 10-question focused session to identify your baseline accuracy in this domain. Read every explanation — even for questions you answer correctly — to understand the reasoning. Once you score consistently above 80%, move to a 20–30 question session to confirm depth before moving to the next domain.
Yes — the session launcher on this page draws questions exclusively from the Exceptions and File I/O domain. Choose 10, 20, 30, or 50 questions for a focused session, or click individual questions to review them one by one.
Save your results, see per-domain analytics, and get readiness scores — free, for every certification.
Sign Up FreeFree forever · Every certification included