PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
Exhibit
Refer to the exhibit.
Error message:
Traceback (most recent call last):
File "script.py", line 3, in <module>
result = "The answer is " + 42
TypeError: can only concatenate str (not "int") to strWhat is the most appropriate fix for the error shown in the exhibit?
⚠ Common exam trap
Candidates may think that only the comma syntax (option B) or only str() (option D) is correct. However, both are valid, but the question asks for the most appropriate fix. The trap is to recognize that str() is the standard way to concatenate different types.
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
✓
Use str(42) to convert the integer to a string.
The error is a TypeError caused by attempting to concatenate a string with an integer using the + operator. The most direct fix is to convert the integer to a string with str(), as shown in option D. While option B (using comma syntax) also resolves the error because print() can accept multiple arguments, the question asks for the 'most appropriate' fix, which typically refers to the explicit type conversion. Option A is incorrect because it claims that option C is a valid fix, which it is not (int() cannot convert a non-numeric string). Option C is invalid. Therefore, option D is the correct answer.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Either A or C are valid fixes.
Why it's wrong here
Incorrect because it claims option C is a valid fix, which is false. While it correctly acknowledges multiple fixes, the specific fixes mentioned are wrong.
- ✗
Use the comma syntax: print("The answer is", 42).
Why it's wrong here
This is a valid fix for the error, as comma syntax avoids concatenation. However, the question asks for the 'most appropriate' fix, and explicit type conversion is more direct for expressing intent.
- ✗
Use int("The answer is ") to convert the string to an integer.
Why it's wrong here
Invalid because int() cannot convert a non-numeric string like 'The answer is' to an integer.
- ✓
Use str(42) to convert the integer to a string.
Why this is correct
Correct. Converting the integer to a string using str() directly resolves the TypeError and is the standard approach for concatenating strings with integers.
Visual reference
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 498 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP 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 PCEP exam.