PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A developer accidentally wrote: print('Hello' + 5). What happens?
⚠ Common exam trap
The trap here is that candidates from languages like JavaScript or PHP, which perform implicit type coercion (e.g., `'Hello' + 5` yields `'Hello5'`), assume Python behaves similarly, but Python's strict typing requires explicit 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
✓
It raises a TypeError
In Python, the `+` operator is overloaded for string concatenation only when both operands are strings. Attempting to concatenate a string (`'Hello'`) with an integer (`5`) violates Python's strong dynamic typing rules, which do not perform implicit type coercion for this operation. This raises a `TypeError` with a message like 'can only concatenate str (not "int") to str'.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
It prints 'Hello' and ignores the 5
Why it's wrong here
Python does not ignore; it errors.
- ✗
It prints a warning but still runs
Why it's wrong here
Error halts execution.
- ✓
It raises a TypeError
Why this is correct
Cannot concatenate str and int.
- ✗
It prints 'Hello5'
Why it's wrong here
This would require str(5).
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 →
Same concept, more angles
2 more ways this is tested on PCEP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A developer writes: print('Hello' + 5). What is the result?
medium- A.Hello 5
- ✓ B.TypeError
- C.SyntaxError
- D.Hello5
Why B: In Python, the + operator performs string concatenation only when both operands are strings. Attempting to concatenate a string ('Hello') with an integer (5) raises a TypeError because Python does not implicitly convert the integer to a string for concatenation. This is a fundamental type safety feature of the language.
Variation 2. A junior developer writes: print('Hello' + 5). This code raises an error. What is the best way to fix it?
medium- A.print('Hello' * 5)
- ✓ B.print('Hello' + str(5))
- C.print('Hello', 5)
- D.print('Hello' + '5')
Why B: Python's `+` operator for strings requires both operands to be strings. The code `print('Hello' + 5)` raises a `TypeError` because it attempts to concatenate a string with an integer. Using `str(5)` converts the integer to the string `'5'`, allowing the concatenation to succeed and print `Hello5`.
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.