PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
Exhibit
>>> x = 10 >>> y = "20" >>> print(x + y) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'int' and 'str'
Refer to the exhibit. A beginner Python programmer executes the code and gets an error. What is the most likely cause?
⚠ Common exam trap
Python Institute often tests the misconception that the + operator can automatically convert types, leading candidates to overlook the need for explicit type conversion or to incorrectly blame the print function.
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
✓
The variables x and y are of incompatible types for the + operator
The error occurs when the + operator is used between incompatible types, such as a string (x) and an integer (y). In Python, the + operator performs concatenation for strings and addition for numbers, but mixing types without explicit conversion raises a TypeError. The code likely defines x as a string (e.g., '10') and y as an integer (e.g., 5), causing the error.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
The variables x and y are of incompatible types for the + operator
Why this is correct
Correct: int and str cannot be added with +
- ✗
The + operator cannot be used with print()
Why it's wrong here
The + operator is valid inside print(); the issue is type mismatch
- ✗
The variable y should be converted to an integer using int()
Why it's wrong here
This is a fix, not the cause of the error
- ✗
The print function is misspelled
Why it's wrong here
print is spelled correctly
Go deeper
Related to this question
About these practice questions
One of 498 original PCEP 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 →
Same concept, more angles
3 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 beginner writes: x = 10; y = "20"; print(x + y). What will happen?
easy- A.It prints 30 as a string
- B.It prints 1020
- C.It prints 30
- ✓ D.It raises a TypeError
Why D: Python does not allow implicit type conversion between a string and an integer in an addition operation. The `+` operator with a string and an integer raises a `TypeError`, as Python's dynamic typing requires explicit conversion (e.g., `int(y)` or `str(x)`) for such mixed-type operations.
Variation 2. A programmer writes: x = 5; y = "10"; z = x + y. What will happen?
easy- A.Prints error but continues execution
- B.Prints 510
- ✓ C.TypeError: unsupported operand type(s) for +: 'int' and 'str'
- D.Prints 15
Why C: Python does not implicitly convert between incompatible types for the '+' operator. When an integer (int) and a string (str) are used with '+', Python raises a TypeError, as it cannot decide whether to perform arithmetic addition or string concatenation without explicit conversion.
Variation 3. Refer to the exhibit. What is the cause of the error?
easy- ✓ A.Using + on incompatible types (str and int)
- B.Division by zero
- C.Missing import statement
- D.Variable not defined
Why A: The error occurs because the `+` operator is being used between a string and an integer, which are incompatible types in Python. Python does not implicitly convert the integer to a string for concatenation; it raises a `TypeError: unsupported operand type(s) for +: 'str' and 'int'`.
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.