Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

A developer is reviewing a script that processes user data. The script contains the following lines:

name = "Alice" age = 30

print(name + " is " + age)

When executed, the script raises a TypeError. Which of the following changes will make the script run without error and produce the output "Alice is 30"?

⚠ Common exam trap

The trap here is assuming that Python automatically converts numbers to strings during concatenation, but the + operator is strict about operand 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

✓

Change the print statement to: print(name + " is " + str(age))

The TypeError arises from attempting to concatenate a string and an integer using the + operator. Python requires both operands to be strings for concatenation. Converting the integer to a string with str() allows the concatenation to succeed. The other options either leave the type mismatch or convert to other numeric types, which still cannot be concatenated with a string.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    Change the print statement to: print(name + " is " + str(age))

    Why this is correct

    The TypeError occurs because the + operator cannot concatenate a string and an integer. Wrapping age with str() converts the integer to its string representation, allowing concatenation. This is the standard way to combine text and numbers in Python when not using f-strings or the format() method. The output will be exactly "Alice is 30".

  • ✗

    Change the print statement to: print(name + " is " + age + " years")

    Why it's wrong here

    Adding more strings does not fix the type mismatch; the expression still attempts to concatenate a string with an integer. The TypeError persists because the integer age is not converted. This option merely adds another string literal but does not address the core issue of mixing types with the + operator.

  • ✗

    Change the print statement to: print(name + " is " + int(age))

    Why it's wrong here

    Using int(age) does not resolve the TypeError because int(age) still returns an integer. The + operator between a string and an integer remains invalid, so the same TypeError occurs. The int() function is used to convert to integer, not to string, so it cannot help with string concatenation in this scenario.

  • ✗

    Change the print statement to: print(name + " is " + float(age))

    Why it's wrong here

    float(age) converts the integer to a floating-point number, but concatenating a string with a float still raises a TypeError. The + operator does not automatically convert non-string types. This change would not produce the desired output and would still result in an error.

About these practice questions

One of 482 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 →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

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.