PCEP Practice Question: Data Types, Variables, Basic I/O and Operators
A student is learning Python and writes a program to compute the area of a rectangle. The code:
length = input("Enter length: ") width = input("Enter width: ") area = length * width
print("Area:", area)When the user enters 5 and 3, the program crashes with a TypeError: can't multiply sequence by non-int of type 'str'. The student is puzzled because they thought input returns numbers. What is the correct explanation and fix?
⚠ Common exam trap
Python Institute often tests the misconception that `input()` returns a number when the user types digits, leading candidates to forget explicit type conversion, and they may incorrectly choose options that suggest renaming variables or using `eval()`.
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, so use int(input("Enter length: ")) and int(input("Enter width: ")).
The `input()` function in Python always returns a string, even if the user types a number. When you try to multiply two strings (or a string by a string), Python raises a `TypeError` because it cannot multiply sequences by non-integers. The fix is to explicitly convert the input to an integer using `int()` before performing arithmetic.
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 input function returns a string, so use int(input("Enter length: ")) and int(input("Enter width: ")).
Why this is correct
Conversion to int allows multiplication.
- ✗
The error is due to variable names length and width conflicting with built-in functions; rename them to l and w.
Why it's wrong here
No conflict; length and width are not built-in functions.
- ✗
The print function cannot handle the multiplication result because area is a string; convert area to int.
Why it's wrong here
area is already the result of multiplication; error occurs before print.
- ✗
The error can be fixed by using the eval function to directly evaluate the input as Python code.
Why it's wrong here
eval is dangerous and not recommended for user input.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-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 →
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 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?
easy- A.ValueError
- B.SyntaxError
- ✓ C.TypeError
- D.NameError
Why C: 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.
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.