Courseiva

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

    The + operator requires both operands to be str; Python raises TypeError rather than silently discarding the integer. It is tempting because some loosely typed languages ignore mismatched operands, and separate print arguments would indeed print 'Hello 5'.

  • ✗

    It prints a warning but still runs

    Why it's wrong here

    Python raises an unhandled TypeError and terminates the statement; it emits no warning and does not continue. It is tempting because dynamically typed languages sometimes warn on questionable operations, but type mismatches in operators are hard errors, not advisories.

  • ✓

    It raises a TypeError

    Why this is correct

    Concatenating a string with an integer using `+` raises a TypeError, because Python's `+` operator requires both operands to be the same sequence type. The string `'Hello'` cannot be implicitly converted to an integer, nor the `5` to a string, so the interpreter halts execution rather than coercing types.

  • ✗

    It prints 'Hello5'

    Why it's wrong here

    Python raises a TypeError because the + operator cannot concatenate str and int objects; it does not coerce the integer. Printing 'Hello5' would require explicit conversion such as str(5) or an f-string. The option tempts because some languages implicitly convert operands, but Python's strong typing forbids it.

About these practice questions

Courseiva writes every PCEP question from scratch — 482 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 →

How Courseiva writes practice questions · Editorial policy

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.