PCAP Exceptions and File I/O Practice Question
A data-import tool must read a UTF-8 CSV file that occasionally contains bytes invalid for UTF-8. The tool should not crash, but it must record that replacement occurred so the operator can review the source. The developer opens the file with `open('data.csv', encoding='utf-8', errors='replace')` and reads it. Which outcome matches this configuration?
⚠ Common exam trap
It's easy for candidates to confuse errors='replace' with errors='ignore', assuming corruption disappears silently rather than leaving the visible U+FFFD replacement character in the decoded string.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Each invalid byte sequence is replaced by U+FFFD, and the tool can scan for that character to detect and count problems.
Passing errors='replace' to open installs an error handler that substitutes U+FFFD for every invalid byte sequence during decoding. The read completes without raising, producing a valid string that still carries visible markers of corruption. Scanning the decoded text for U+FFFD lets the tool count and report affected records, satisfying both the no-crash and traceability requirements.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Each invalid byte sequence is replaced by U+FFFD, and the tool can scan for that character to detect and count problems.
Why this is correct
The replace error handler substitutes the Unicode replacement character U+FFFD wherever the decoder encounters an invalid sequence. The resulting string is valid and the read does not raise, so the tool can search for U+FFFD to count and locate suspect regions. This matches the requirement to continue processing while recording that replacement occurred.
- ✗
Invalid bytes are silently removed from the decoded string, leaving no trace in the text.
Why it's wrong here
Silent removal is the behavior of errors='ignore', not errors='replace'. With ignore the offending bytes vanish entirely, which would make operator review impossible. The configured handler substitutes a visible replacement character, so this description belongs to a different error strategy.
- ✗
The decoder substitutes a question mark character and logs a warning through the warnings module automatically.
Why it's wrong here
The replacement character is U+FFFD, not a question mark, and the codec does not emit warnings on its own. Any logging must be implemented by the developer. This option combines two inaccuracies, the substituted character and an automatic warning, that do not reflect how errors='replace' behaves.
- ✗
Decoding invalid bytes raises UnicodeDecodeError, and the handler in the tool catches it per line.
Why it's wrong here
The errors='replace' argument changes the behavior so that invalid byte sequences do not raise UnicodeDecodeError. Instead the decoder substitutes a replacement character. Relying on an exception handler would therefore never trigger under this configuration, so this outcome contradicts the chosen error handler.
Go deeper
Related to this question
About these practice questions
One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
JA
Written and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Python Institute exam blueprint
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.