Courseiva
Computer Programming and Python FundamentalshardMultiple ChoiceObjective-mapped

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

    Not without global declaration.

  • Built-in

    Why it's wrong here

    For built-in functions.

  • Local

    Why this is correct

    Limited to the function.

  • Nonlocal

    Why it's wrong here

    For enclosing function scopes.

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 →

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. 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: To modify a global variable inside a function, the global keyword must be used to declare the variable as global.

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.