PCAP Exceptions and File I/O Practice Question
A developer opens a CSV file with open('data.csv', 'r', encoding='utf-8') and reads it with readline(). The first line contains a non-ASCII character that the file declares as UTF-8. Which exception is most likely raised if the file actually contains bytes that are not valid UTF-8?
⚠ Common exam trap
The trap here is mixing up UnicodeDecodeError and UnicodeEncodeError, which apply to opposite directions of text conversion.
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
✓
UnicodeDecodeError
Reading text from a file opened with a declared encoding performs decoding, so invalid byte sequences raise UnicodeDecodeError. The other exceptions describe unrelated conditions: encoding errors happen on write, syntax errors happen at parse time, and ValueError is a generic mismatch. Handling the decode error lets the program recover from malformed data.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
UnicodeDecodeError
Why this is correct
When a file is opened in text mode with an explicit encoding, reading bytes that do not form valid sequences for that encoding raises UnicodeDecodeError, a subclass of UnicodeError. In this scenario the declared encoding is UTF-8, so invalid UTF-8 byte sequences trigger this exception during readline(). Handling it allows the program to log the offending position or fall back to a different decoding strategy.
- ✗
ValueError
Why it's wrong here
ValueError is raised when a built-in operation receives an argument of the right type but an inappropriate value, such as int('abc'). While some decoding problems can be wrapped in ValueError in other libraries, the built-in text file reader raises UnicodeDecodeError for invalid byte sequences. Catching ValueError here would be too broad and would not specifically identify the decoding failure.
- ✗
SyntaxError
Why it's wrong here
SyntaxError is raised when Python source code cannot be parsed, such as a missing colon or mismatched parentheses. It has no relationship to reading data from a CSV file at runtime. A malformed byte sequence in a data file does not affect parsing of the script, so this exception cannot be the one raised by readline() in this scenario.
- ✗
UnicodeEncodeError
Why it's wrong here
UnicodeEncodeError occurs when converting a str to bytes, such as when writing text to a file whose encoding cannot represent a character. The scenario is reading bytes from a file, which is a decoding operation, so an encode error is not the relevant failure. Confusing the two directions is a common mistake, but only decoding errors apply when reading text from an existing file.
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.