20+ practice questions focused on Exceptions and File I/O — one of the most tested topics on the Certified Associate Python Programmer PCAP exam. Each question includes a detailed explanation so you learn why the right answer is correct.
Start Exceptions and File I/O PracticeA 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?
Explanation: It places the try-except block inside the loop, specifically catching `UnicodeDecodeError` for each line. This allows the script to skip only the problematic line and continue processing subsequent lines, which matches the requirement exactly.
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?
Explanation: The `Exception` class's `__init__` method stores the message argument in the `args` attribute, which is used by the default `__str__` method to display the message. By overriding `__init__` to accept a message and call `super().__init__(message)`, the custom exception properly passes the message to the base class, ensuring it is displayed when caught and printed.
Which TWO of the following are valid ways to raise an exception in Python?
Explanation: In Python, `raise Exception` is valid syntax; it raises an instance of `Exception` with no arguments. `raise Exception('error')` and `raise ValueError('invalid value')` are also valid. Therefore, the correct answer choices are B, D, and E, not just D and E.
What is the output of the code? ```python with open('log.txt') as f: for line in f: if 'ERROR' in line: print('Error found:', line) break ```
Explanation: The code (which is not shown in the question) reads a log file line by line. It checks if each line contains the substring 'ERROR'. Upon finding the first such line '2025-03-15 10:23:45 ERROR: Division by zero in module calc.py line 42', it prints 'Error found: ' followed by that line and then breaks out of the loop. Therefore, only that line is printed, matching option D.
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?
Explanation: The most likely cause is that the external process manipulating the file leads to an invalid file descriptor. Catching OSError and re-opening the file provides a fresh descriptor and allows the script to continue without relying on the external process to use file locking. This approach is simpler and more robust in environments where the external process may not support locking.
+15 more Exceptions and File I/O questions available
Practice all Exceptions and File I/O questions1. Baseline your knowledge
Start with 10 questions to gauge your current understanding of Exceptions and File I/O. This tells you whether you need a concept refresher or just practice.
2. Review every explanation
For each question — right or wrong — read the full explanation. Understanding why an answer is correct is more valuable than knowing the answer itself.
3. Focus on exam traps
Exceptions and File I/O questions on the PCAP frequently use trap wording. Look for subtle differences in answers that test your precision, not just general knowledge.
4. Reach 80% consistently
Do repeated sessions until you score 80%+ three times in a row. Then move to mixed-mode practice to test cross-topic recall under realistic conditions.
The exact number varies per candidate. Exceptions and File I/O is tested as part of the Certified Associate Python Programmer PCAP blueprint. Practicing with targeted Exceptions and File I/O questions ensures you can handle any format or difficulty that appears.
Yes. Courseiva provides free PCAP practice questions across all exam topics and domains. The platform includes topic-based practice, mock exams, missed-question review, bookmarked questions, and readiness tracking — no account required.
Difficulty is subjective, but Exceptions and File I/O is a high-priority exam concept tested in multiple ways — direct recall, scenario analysis, and command-output interpretation. Consistent practice is the best way to build confidence.
Launch a full Exceptions and File I/O practice session with instant scoring and detailed explanations.
Start Exceptions and File I/O Practice →