PCAP Exceptions and File I/O Practice Question
Exhibit
[ERROR] 2025-01-01 12:00:00: Unhandled exception: ValueError: invalid literal for int() with base 10: 'abc'
Refer to the exhibit. Which of the following Python code snippets would generate this error?
⚠ Common exam trap
Python Institute often tests the exact wording of Python error messages, so the trap here is that candidates may confuse `ValueError` from `int()` with `ValueError` from `float()`, but the error message in the exhibit specifically cites 'invalid literal for int()', making only `int('abc')` the correct match.
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
✓
int('abc')
`int('abc')` attempts to convert the string `'abc'` to an integer, which is not a valid numeric literal. Python raises a `ValueError` with the message 'invalid literal for int() with base 10: 'abc''. This error occurs because the `int()` function expects a string that represents a valid integer in the specified base (default base 10), and 'abc' does not meet that criterion.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
int('abc')
Why this is correct
int('abc') invokes Python's integer constructor on the non-numeric string 'abc'. Since the string does not contain a valid integer literal—even after stripping surrounding whitespace or handling an optional sign—the conversion fails, raising a ValueError with the exact message "invalid literal for int() with base 10: 'abc'". This is the precise exception and message that the exhibit/question targets, making this the correct snippet.
- ✗
str(123)
Why it's wrong here
str(123) calls the string constructor with the integer object 123, which is a perfectly valid operation. The function returns a new string containing the decimal representation '123' without attempting any parsing or numeric conversion. Because str() accepts any Python object and simply produces its printable string form, no exception is raised, so this snippet cannot be the one that generates a ValueError.
- ✗
print('abc')
Why it's wrong here
print('abc') is a built-in function that writes the given object to standard output, preceded by an optional separator and followed by a newline. It performs no type conversion, validation, or arithmetic; the object is passed directly to the output machinery and converted to text via its __str__ method if needed. Since print() is designed to handle any object without failing, it terminates successfully and does not produce any exception, let alone a ValueError.
- ✗
float('abc')
Why it's wrong here
float('abc') does attempt to convert the string 'abc' to a floating-point number and, like int('abc'), it raises a ValueError because the string is not a valid float literal. However, the exception message differs: Python reports "could not convert string to float: 'abc'" rather than the integer-specific message. Therefore, while this snippet does throw a ValueError, it is not the exact error message required by the exhibit, making it an incorrect choice for this question.
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 by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
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.