Courseiva
Data Types, Variables, Basic I/O and OperatorsmediumMultiple ChoiceObjective-mapped

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

A programmer is writing a script to read a number, determine if it is even or odd, and then also use the number to calculate its square. The code:

num = input("Enter a number: ")

if num % 2 == 0:
    print("Even")

else:

print("Odd")

square = num ** 2

print("Square:", square)

When run, a TypeError occurs on the modulo line. Which fix will resolve the error and allow the later calculation to work?

⚠ Common exam trap

Python Institute often tests the misconception that converting the input type only on the line where the error occurs is sufficient, but the trap is that the variable remains a string for subsequent operations, so a single conversion at assignment is the correct fix.

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 input to: num = int(input("Enter a number: "))

The `input()` function always returns a string, and the modulo operator `%` and exponentiation operator `**` require numeric operands. By converting the input to an integer with `int(input(...))`, both the modulo and exponentiation operations work correctly, resolving the TypeError and allowing the square calculation to proceed.

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 condition to: if num // 2 == 0:

    Why it's wrong here

    num is a string; // does not work on strings.

  • Change the condition to: if float(num) % 2 == 0:

    Why it's wrong here

    float works for modulo, but num remains a string for square, causing error.

  • Change the condition to: if int(num) % 2 == 0:

    Why it's wrong here

    This fixes modulo but num is still a string for the square calculation.

  • Change the input to: num = int(input("Enter a number: "))

    Why this is correct

    This converts to int at the source, so all operations work.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

About these practice questions

Courseiva writes every PCEP question from scratch — 498 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 junior developer is writing a script to read a number from input and double it. They write: num = input("Enter a number: ") result = num * 2 print(result) When they test with input 5, the output is '55' instead of 10. What is wrong?

easy
  • A.There is a syntax error in the multiplication line.
  • B.The print function is incorrectly formatting the output.
  • C.The variable name 'num' conflicts with a built-in function.
  • D.The input is treated as a string; they need to convert it to int.

Why D: The `input()` function in Python always returns a string, even if the user types a number. When you use the `*` operator on a string, it repeats the string, so `'5' * 2` yields `'55'`. To perform numeric multiplication, you must convert the input to an integer using `int(input(...))`.

Variation 2. A student tries to write a program that prints the square of a number. The code: num = 5 print("The square is " + num ** 2) When run, a TypeError occurs. Which of the following fixes the error and produces exactly the output 'The square is 25'?

easy
  • A.Change to: print("The square is " + str(num ** 2))
  • B.Change to: print("The square is " + num ** 2)
  • C.Change to: print("The square is", num ** 2)
  • D.Change to: print("The square is " + (num ** 2))

Why A: It converts the integer result of `num ** 2` (which is 25) to a string using `str()`, allowing concatenation with the string literal `"The square is "`. Without this conversion, Python raises a `TypeError` because the `+` operator cannot concatenate a string and an integer directly.

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.