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 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?
Explanation: Option A is correct because `FileNotFoundError` is a built-in exception in Python that is raised when a file or directory is requested but does not exist. In Python 3, file-related I/O errors are organized under `OSError` with specific subclasses, and `FileNotFoundError` is the precise exception for a missing file, making it the most appropriate catch for this scenario.
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?
Explanation: Option C is correct because 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: Option A is correct because 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 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?
Explanation: Option B is correct because the 'w' mode opens the file for writing in text mode and truncates the file to zero length if it exists, or creates a new file if it does not. This ensures any existing content is overwritten, which matches the requirement.
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?
Explanation: The `read(n)` method reads exactly `n` bytes from the file object when the file is opened in binary mode (`'rb'`). Since the question specifies reading exactly 4 bytes, `f.read(4)` is the correct and direct approach. This method returns a bytes object of up to `n` bytes, but if the file has at least 4 bytes remaining, it will return exactly 4.
+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 →