Courseiva

PCEP TypeError Practice Question

A developer writes code to calculate the area of a rectangle and prints it. The code is: length = 10 width = 5 area = length + width

print('The area is', area)

If the width is accidentally assigned a string '5', what error will occur?

⚠ Common exam trap

The trap is that candidates may incorrectly assume that Python allows integer + string for arithmetic addition, but actually it raises a TypeError. The key is to recognize that Python does not perform implicit type conversion in this context.

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

✓

TypeError

In Python, adding an integer and a string is not a valid operation; it will raise a TypeError because Python cannot implicitly convert the string to an integer for arithmetic. Therefore, the code will raise a TypeError when executed.

Answer analysis

Option-by-option breakdown

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

  • ✗

    ValueError

    Why it's wrong here

    ValueError arises when a value has the right type but an invalid one, such as int('abc'). Here the operand types differ, so Python raises TypeError on the int plus str addition. ValueError would be the answer if the code attempted an int() conversion of non-numeric text.

  • ✗

    SyntaxError

    Why it's wrong here

    SyntaxError is raised during parsing when the code's grammar is invalid, before any assignment executes. The statement width = '5' is syntactically valid; the failure occurs at runtime when adding int and str. SyntaxError would apply to malformed code such as a missing colon or unmatched parenthesis.

  • ✓

    TypeError

    Why this is correct

    Assigning `'5'` makes `width` a string, so `length + width` attempts to add an integer and a string. Python does not implicitly coerce operands across these types, so the addition raises a TypeError before `print` runs. This satisfies the stem's scenario of accidental string assignment, where the failure occurs at the arithmetic operation itself.

  • ✗

    NameError

    Why it's wrong here

    NameError occurs when a variable is referenced before assignment or is undefined. Both length and width are assigned before area is calculated, so no undefined name exists. NameError would be correct if, for example, the code printed a variable that was never created.

About these practice questions

This PCEP question is part of Courseiva's 482-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.