Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

Exhibit

Refer to the exhibit.

Traceback (most recent call last):
  File "script.py", line 3, in <module>
    print(result)
NameError: name 'result' is not defined

A developer runs the code from the exhibit and gets the error shown. Which of the following is the most likely cause?

⚠ Common exam trap

The PCEP exam often tests the distinction between a variable that exists but has the wrong type (TypeError) and a variable that has never been assigned (NameError), leading candidates to incorrectly focus on type mismatches instead of the missing assignment.

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 variable 'result' was never assigned a value.

The error message indicates that the variable 'result' is referenced before it has been assigned any value. In Python, using a variable that has never been assigned raises a NameError. The code attempts to print 'result', but no assignment to 'result' exists in the provided code, so Python cannot resolve the name.

Answer analysis

Option-by-option breakdown

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

  • ✗

    There is a typo in the variable name.

    Why it's wrong here

    A misspelt variable name produces NameError only when that identifier is referenced; the exhibited error would name the offending identifier explicitly. The option is tempting because typos are a frequent beginner mistake. The traceback's actual message points to the genuine cause rather than a naming mismatch.

  • ✓

    The variable 'result' was never assigned a value.

    Why this is correct

    Referencing a name that was never bound raises `NameError`, which is exactly what the exhibit shows. Python resolves names at runtime, so `result` must be assigned before use; an unassigned variable has no object to return. This satisfies the stem's constraint that the traceback names `result` as undefined.

  • ✗

    The print function requires an import.

    Why it's wrong here

    print is a built-in function available without any import, so an ImportError would not arise from calling it. The option is tempting because many Python features do require imports, such as math or sys. The exhibited error instead stems from the actual cause shown in the traceback.

  • ✗

    The variable 'result' is a string, not an integer.

    Why it's wrong here

    Python does not raise an error for a string variable unless it is used in an unsupported operation; assignment alone is valid. The option is tempting because type mismatches commonly cause TypeErrors. The exhibited traceback indicates a different fault, such as a name or syntax error.

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 — 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. Refer to the exhibit. What is the most likely cause of this error?

easy
  • A.Variable x is a string
  • ✓ B.Variable x is used before assignment
  • C.Variable x is an integer
  • D.Variable x is misspelled

Why B: 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.

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.