Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

Consider the following function definition:

def add(a, b):
    return a + b

What is the value of add(3, '4')?

⚠ Common exam trap

The PCEP exam often tests the misconception that Python will implicitly convert types (like treating `'4'` as an integer) or that the `+` operator will concatenate any two values, when in reality it raises a `TypeError` for incompatible 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

✓

TypeError: unsupported operand type(s) for +: 'int' and 'str'

Python's `+` operator is not defined for mixed types like `int` and `str`. When `add(3, '4')` is called, Python attempts to evaluate `3 + '4'`, which raises a `TypeError` because it cannot implicitly convert the string to an integer or vice versa. This is a fundamental type safety rule in Python.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Error due to missing import

    Why it's wrong here

    Python raises a TypeError because the + operator cannot concatenate an int with a str; no import is involved, and the function itself is defined correctly. A missing-import error is tempting because NameError often signals an unimported module, but here the failure is operand type mismatch, not a missing name.

  • ✗

    '34'

    Why it's wrong here

    Concatenation cannot occur because Python raises a TypeError when adding int and str; the '+' operator requires matching types. It tempts because string concatenation via '+' is valid when both operands are strings, as in add('3', '4').

  • ✗

    7

    Why it's wrong here

    Integer addition fails because '4' is a string, so Python raises a TypeError rather than coercing it. It tempts because add(3, 4) with two integers returns 7, making numeric addition the expected result when both arguments are numbers.

  • ✓

    TypeError: unsupported operand type(s) for +: 'int' and 'str'

    Why this is correct

    Python's + operator does not coerce types, so adding int 3 to str '4' raises TypeError. The operands must share a compatible type for concatenation or addition, and no implicit conversion occurs between int and str.

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 function is defined as: def add(a, b=5): return a + b What is the result of add(10)?

medium
  • A.10
  • B.5
  • ✓ C.15
  • D.Error

Why C: The function `add(a, b=5)` defines a default value of 5 for parameter `b`. When called as `add(10)`, the argument 10 is assigned to `a`, and `b` uses its default value of 5. The function returns `10 + 5 = 15`, making option C correct.

Variation 2. A developer writes a function that should return the sum of two numbers, but the code returns 0 instead. What is the most likely cause? def add(a, b): result = a + b print(add(3, 4))

easy
  • A.The function is not defined before the call.
  • B.The variable 'result' is not defined.
  • C.The function parameters are of incompatible types.
  • ✓ D.The function does not have a return statement.

Why D: The function `add` computes `a + b` and assigns it to `result`, but lacks a `return` statement. In Python, a function without an explicit `return` automatically returns `None`. When `print(add(3, 4))` is executed, it prints `None`, not the sum. The question states the code returns 0, which is a common misreading — the actual output is `None`, but the core issue is the missing `return`.

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.