PCEP Computer Programming and Python Fundamentals Practice Question
What is the scope of a variable defined inside a function?
⚠ Common exam trap
The PCEP exam often tests the misconception that any variable inside a function is automatically global or that 'nonlocal' applies to simple (non-nested) functions, leading candidates to pick 'Global' or 'Nonlocal' instead of 'Local'.
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
✓
Local
In Python, any variable assigned inside a function (without global or nonlocal declarations) has local scope. This means it is only accessible within that function's block and is destroyed when the function returns, as per Python's LEGB (Local, Enclosing, Global, Built-in) rule.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Global
Why it's wrong here
A name assigned inside a function is local to that function unless declared global; it is not global by default. It is tempting because module-level names are global, and a question about a variable defined at file top level would make global the correct answer.
- ✗
Built-in
Why it's wrong here
Built-in scope belongs to names predefined by the interpreter, not to variables assigned inside a function. It is tempting because built-ins are reachable from within functions, yet a question about where a name is defined, rather than where it is visible, would make built-in correct.
- ✓
Local
Why this is correct
Variables created inside a function exist only within that function's local namespace, which Python binds at call time and discards on return. This satisfies the stem's scope constraint: the name is unreachable from module level or any other function, so it cannot leak or collide with globals of the same name.
- ✗
Nonlocal
Why it's wrong here
Nonlocal binds a name to an enclosing function's scope, so it applies only to nested functions with an explicit nonlocal declaration. It is tempting because nonlocal does concern function-local scoping, and a question about rebinding a variable in an enclosing function would make it correct.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 482-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 a function that modifies a global variable inside the function: count = 0 def increment(): count += 1 When called, an error occurs. What is the correct way to fix this?
medium- A.Define count inside the function
- B.Use the 'static' keyword
- C.Pass count as an argument to the function
- ✓ D.Use 'global count' inside the function
Why D: The error occurs because Python treats 'count' as a local variable inside the function due to the assignment, but it is referenced before assignment. Using 'global count' inside the function explicitly tells Python to use the global variable, allowing modification. This is the correct fix for modifying a global variable from within a function.
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.