Courseiva
Computer Programming and Python FundamentalsmediumMultiple ChoiceObjective-mapped

PCEP Computer Programming and Python Fundamentals Practice Question

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?

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

Use 'global count' inside the function

To modify a global variable inside a function, the global keyword must be used to declare the variable as global.

Answer analysis

Option-by-option breakdown

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

  • Define count inside the function

    Why it's wrong here

    Defining count locally would create a new local variable, not modify the global.

  • Use the 'static' keyword

    Why it's wrong here

    Python has no static keyword for variable scope.

  • Pass count as an argument to the function

    Why it's wrong here

    Passing as argument would not alter the original global variable unless reassigned and returned.

  • Use 'global count' inside the function

    Why this is correct

    The global statement allows the function to modify the global variable.

About these practice questions

One of 498 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.