Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

Consider the code: x = 10; def func(): x = 5; print(x); func(); print(x). What is the output?

⚠ Common exam trap

The PCEP exam often tests the misconception that a variable assignment inside a function modifies the global variable, leading candidates to incorrectly choose options where both outputs are the same or the global value is overwritten.

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

✓

5 10

Inside the function `func()`, the local variable `x` is assigned the value 5, so `print(x)` inside the function outputs 5. After the function call, the global `x` remains 10, so the final `print(x)` outside the function outputs 10. This demonstrates Python's scoping rules where assignments inside a function create a local variable unless declared 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.

  • ✓

    5 10

    Why this is correct

    Assigning `x = 5` inside `func` creates a new local variable, so the printed value within the function is 5. The module-level `x` remains 10, since Python resolves names by local-then-global scope and the assignment never rebinds the global. Hence the output is 5, then 10.

  • ✗

    5 5

    Why it's wrong here

    Printing 5 then 5 ignores Python's LEGB scope resolution: the assignment inside func() creates a local x, so the first print outputs 5, but the module-level x remains 10, making the second print output 10. It tempts those who assume reassignment inside a function mutates the global binding without a global declaration.

  • ✗

    10 10

    Why it's wrong here

    Printing 10 then 10 overlooks that func()'s local assignment shadows the global x, so its print outputs 5, not 10. It tempts candidates who correctly recall that the module-level x stays 10 after the call, then wrongly apply that value to both print statements instead of only the second.

  • ✗

    10 5

    Why it's wrong here

    The function assigns x = 5 locally, so func() prints 5, while the module-level x remains 10, printing 10 afterwards. This option reverses that order, which would require the local assignment to leak into the global scope, something Python does not do without a global declaration.

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 →

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.