Courseiva

PCEP Practice Question: Data Types, Variables, Basic I/O and Operators

A student writes the following code to calculate the average of two numbers: ```python num1 = input("Enter first number: ") num2 = input("Enter second number: ") avg = (num1 + num2) / 2

print("Average:", avg)

``` When executed, the code raises a TypeError. What is the most likely cause?

⚠ Common exam trap

Python Institute often tests the misconception that `input()` returns a numeric type when the user types digits, leading candidates to overlook the need for explicit type conversion with `int()` or `float()`.

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 input() function returns a string, not a number, so arithmetic operations are invalid without conversion

The `input()` function in Python always returns a string, regardless of what the user types. Attempting to use the `+` operator on two strings performs concatenation, not numeric addition, and then dividing a concatenated string by an integer with `/` raises a `TypeError` because the `/` operator is not defined for strings. To fix this, the inputs must be explicitly converted to numbers using `int()` or `float()` before arithmetic operations.

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 division operator '/' is not allowed for integers

    Why it's wrong here

    Python's '/' operator handles integers without error, returning a float. The actual failure is that input() yields strings, so num1 + num2 concatenates before division. This option misidentifies the operator as the problem rather than the operand types.

  • ✓

    The input() function returns a string, not a number, so arithmetic operations are invalid without conversion

    Why this is correct

    The `input()` function always returns a string, so `num1 + num2` performs string concatenation rather than numeric addition, and dividing that concatenated string by 2 raises a TypeError. Converting both values with `int()` or `float()` before the arithmetic satisfies the requirement for numeric operands.

  • ✗

    The addition operator '+' cannot be used with strings

    Why it's wrong here

    input() returns strings, so num1 + num2 concatenates them; dividing that string by 2 then raises the TypeError. The '+' operator itself works fine on strings — it is the subsequent division that fails, making this a tempting but imprecise diagnosis.

  • ✗

    The variable names num1 and num2 are reserved keywords

    Why it's wrong here

    `num1` and `num2` are ordinary identifiers, not reserved keywords, so naming cannot raise a TypeError. The failure comes from `input()` returning strings, making `num1 + num2` concatenate rather than add, then dividing a string by 2. This option tempts candidates who confuse keyword restrictions with runtime type errors.

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

1 more way 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 junior developer writes: result = input("Enter first: ") + input("Enter second: ") and then prints result. When entering 5 and 3, the output is '53'. Which explanation is correct?

easy
  • A.The code uses the wrong operator; it should use the & operator for addition.
  • B.The input() function automatically selects the correct type, but the + operator is overloaded to concatenate.
  • C.Python automatically converts strings to numbers when using +, but only for integers.
  • ✓ D.The input() function returns a string, so + performs string concatenation.

Why D: The `input()` function in Python always returns a string, regardless of what the user types. When the `+` operator is used with two strings, it performs concatenation, not arithmetic addition. Thus, entering 5 and 3 results in the strings '5' and '3' being joined to produce '53'.

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.