Scoping: One Mark Questions, Book Back Answers and Notes
Scope is the part of a program where a variable can be used. This unit covers mapping and namespaces, lifetime, the four types of scope and the LEGB rule, modular programming and its benefits, and access control with public, protected and private members in Python, C++ and Java.
Study this chapter
Asked in previous board exams
6 questions from this chapter came in 6 past public exams. Open any of them to see the answer and explanation.
-
March 2026
Which members are accessible from outside the class?See answer in the solved paper →
-
March 2025
The __________ rule is used to decide the order in which the scopes are to be searched.See answer in the solved paper →
-
March 2024
Which of the following security technique that regulates who can view or use resources in a computing environment ?See answer in the solved paper →
-
May 2022
Containers for mapping names of variables to object is called :See answer in the solved paper →
-
Supplementary 2021
The process of binding a variable name with an object is called :See answer in the solved paper →
-
March 2020
Which of the following members of a class can be handled only from within the class ?See answer in the solved paper →
About Scoping
Medium ~45 min study
Scope is the part of a program where a variable name can be accessed. If every name were visible everywhere, a variable called x in one function would clash with an x in another, and one would overwrite the other. Scoping rules stop this by giving each block of code its own set of names.
The chapter starts with how a name is mapped to an object and how namespaces keep track of these mappings. It then lists the four scopes (Local, Enclosed, Global and Built-in) and the LEGB rule the interpreter uses to decide which variable a name refers to. From there it moves to modular programming, where a program is split into modules, and finishes with access control: how C++ and Java use public, protected and private, and how Python imitates them with underscores.
The most common exam task here is reading a short program with nested functions and saying what gets printed. To do it, look for the name in the innermost function first and work outwards, one scope at a time. Also learn the list of benefits of modular programming and the underscore convention for Python class members.
What you'll learn
- Define scope, lifetime and namespace in your own words.
- Apply the LEGB rule to find which variable a name refers to in a nested program.
- Identify local, enclosed, global and built-in variables in a given code sample.
- List the benefits of modular programming.
- Explain how access control keeps class members from being used freely.
- Compare how C++, Java and Python handle public, protected and private members.
Before you start
- Variables, expressions and data types.
- Defining and calling simple functions.
- A basic idea of classes and objects.
Topics covered in this chapter
Scoping explained
Scope Rules, Modules and Access Control
Mapping, Namespaces and Lifetime
A variable name is a way of referring to an object in memory. Binding a name to an object is called mapping, and in Python the assignment operator = does this: a = 5 maps the name a to the object 5. Namespaces are containers that hold these mappings, like a dictionary where the names are keys and the objects are values. The lifetime of a variable is the time for which it exists in memory. A local variable's lifetime ends when its function finishes.
The LEGB Rule
When the same name exists in several places, the interpreter follows the LEGB rule to find the right one. It searches in this order: Local, Enclosed, Global, Built-in. It stops at the first scope where the name is found, so a local variable hides a global variable with the same name.
The Four Types of Scope
Local scope covers variables defined inside the current function; they can be used only there. Enclosed scope appears with nested functions: an inner function can read the variables of the function that contains it. Global scope covers variables declared outside all functions, which can be used anywhere in the program. Built-in scope is the widest; it holds names that are pre-loaded when the interpreter starts, such as print and len.
Modular Programming
Modular programming divides a large program into smaller, independent sub-programs called modules. The textbook lists these benefits: less code has to be written; a single procedure can be reused many times; programs can be designed more easily because a small team works on each part; errors are confined to one module, so debugging is simpler; code can be kept in multiple files; the code is short, simple and easy to understand; and scoping is easy to manage.
Access Control
Access control is a security technique that regulates who or what can view or use resources in a computing environment. C++ and Java control access to class members with the keywords public, protected and private. Python has no such keywords, and all members of a Python class are public by default. To imitate protected and private members, Python uses a naming convention: a single underscore prefix (_name) for protected and a double underscore prefix (__name) for private.
Common mistakes to avoid
- Using a local variable outside its function. Python raises NameError; return the value from the function instead.
- Expecting a variable made in an inner function to be visible to the outer function or the whole program. Scope only reaches inwards, not outwards.
- Assigning to a global variable inside a function without declaring it global. Python then creates a new local variable and the global one stays unchanged.
- Assuming Python blocks access to private members the way C++ and Java do. Python only uses the underscore naming convention to imitate private and protected access.
Test yourself on these with the practice test, then check the worked reasoning in the solved MCQs.
Frequently asked questions
What is the difference between the scope and lifetime of a variable?
Scope is the part of the program where the variable can be used. Lifetime is how long the variable exists in memory while the program runs. A local variable has the function as its scope, and its lifetime ends when the function returns.
How does the LEGB rule resolve a variable name?
The interpreter looks for the name in the Local scope first, then in any Enclosing function, then in the Global scope, and last in the Built-in scope. It uses the first match it finds. If the name is in none of them, you get an error.
What is enclosed scope? Give an example.
Enclosed scope comes from a function defined inside another function. If disp() defines a = 7 and contains a function disp1() that prints a, disp1() prints 7, because it finds a in the enclosing function disp().
How do I change a global variable inside a Python function?
You can read a global variable inside a function without doing anything special. To assign a new value to it, write global followed by the variable name inside the function first. Without that line, the assignment creates a new local variable.
What are the benefits of modular programming?
Less code has to be written, one procedure can be reused many times, and a small team can work on each module. Errors stay inside one module, which makes debugging easier. Code can be stored in several files and is shorter and easier to understand.
How does Python handle private and protected members?
Python has no public, protected or private keywords, and class members are public by default. By convention, a name starting with a single underscore is treated as protected and one starting with a double underscore as private. This imitates the access specifiers of C++ and Java.
What is a namespace?
A namespace is a container that maps names to objects. Think of it as a dictionary: the variable names are the keys and the objects they refer to are the values.
Last updated 15 September 2026