Courseiva
Functions, Tuples, Dictionaries and ExceptionsmediumMultiple ChoiceObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A junior developer wrote a function that calculates the average of a list of numbers. Inside the function, they used a variable named 'list' to store the input parameter. Later, they tried to call the built-in list() function to convert a string to a list inside the same function, but it raised a TypeError. The error occurs because the name 'list' now refers to the parameter, not the built-in. The function must be fixed without changing its external behavior. Which solution is the best practice?

⚠ Common exam trap

The PCEP exam often tests the concept of name shadowing, where candidates mistakenly think that using the 'global' keyword or importing builtins is the proper fix, instead of simply renaming the local variable to avoid shadowing the built-in function.

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

Rename the local variable to something else, like 'lst' or 'data'

The best practice is to avoid shadowing built-in names. By renaming the parameter from 'list' to something like 'lst' or 'data', the built-in list() function remains accessible, and the function's external behavior is unchanged. This approach is simple, readable, and follows Python's naming conventions.

Answer analysis

Option-by-option breakdown

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

  • Use the global keyword to refer to the built-in list

    Why it's wrong here

    The built-in list is not in the global scope; it's in the builtins module. global does not solve the shadowing.

  • Use the builtins module (import builtins; builtins.list()) to call the built-in

    Why it's wrong here

    Using `builtins.list()` would technically allow access to the shadowed built-in function, resolving the `TypeError`. However, this is not considered best practise for this scenario because it circumvents, rather than fixes, the underlying issue of a poorly named local variable shadowing a built-in. The `builtins` module is useful for introspection or when a built-in name is *unavoidably* shadowed, but for simple name clashes like this, renaming the local variable is the cleaner, more Pythonic solution.

  • Rename the local variable to something else, like 'lst' or 'data'

    Why this is correct

    Renaming avoids shadowing the built-in and is the recommended practice.

  • Remove the local variable and use the input parameter directly

    Why it's wrong here

    The function already uses the parameter; removing the variable may not be possible if the parameter is also named 'list'.

About these practice questions

Courseiva writes every PCEP question from scratch — 498 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

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.