PCEP Computer Programming and Python Fundamentals Practice Question
Exhibit
NameError: name 'x' is not defined
Refer to the exhibit. What is the most likely cause of this error?
⚠ Common exam trap
The PCEP exam often tests the distinction between a variable being undefined versus being of a certain type, tricking candidates into thinking the error is about type mismatch when it is actually about the variable not existing yet.
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
✓
Variable x is used before assignment
The error message indicates that variable 'x' is being referenced before it has been assigned a value. In Python, using a variable that has not been defined yet raises a NameError. Option B correctly identifies this as the cause.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Variable x is a string
Why it's wrong here
Concatenating a string with an integer raises TypeError, but the stem's error stems from calling an arithmetic operation on a str, not from x merely being a string. Strings are tempting because they support indexing and iteration, yet they cannot participate in numeric addition without explicit int() conversion.
- ✓
Variable x is used before assignment
Why this is correct
Python raises UnboundLocalError when a name is assigned somewhere in a function, making it local throughout, yet read before that assignment executes. The traceback points to a read of x occurring before its first assignment on that code path.
- ✗
Variable x is an integer
Why it's wrong here
An integer would permit arithmetic operations, so it cannot produce the TypeError shown; the failing operand is non-numeric. Integers are tempting because they are the default numeric type in Python and work with +, -, and *, but the exhibit's traceback points to a str operand instead.
- ✗
Variable x is misspelled
Why it's wrong here
A misspelt variable name raises NameError only when the name is undefined at runtime; it does not produce the syntax error shown in the exhibit, which the parser detects before execution. Misspelling would be the likely cause if the traceback reported NameError instead.
Go deeper
Related to this question
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 →
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.