Chapter 3: Scoping — Online MCQ Test
COMPUTER SCIENCE · CLASS 12th · Tamil Nadu State Board
Practice Chapter 3: Scoping with a free chapter-wise online MCQ test.
This chapter covers: This chapter details variable accessibility and lifespan within different program parts. It covers local global module and built-in scopes using the LEGB rule along with namespaces....
AI-generated questions from basic to board-exam level, with instant results and explanations.
Chapter 3: Scoping — Important Questions & Answers
What does the term 'scope' in programming refer to?
- A. The speed of program execution
- B. The region where a variable can be accessed
- C. The size of the data type
- D. The number of functions in a program
Answer: B. The region where a variable can be accessed
Scope means the part of a program where a variable, function, or name is visible and can be used.
Scope means the part of a program where a variable, function, or name is visible and can be used.
Which scope is created when a variable is defined inside a function?
- A. Global scope
- B. Local scope
- C. Built-in scope
- D. Module scope
Answer: B. Local scope
A variable declared inside a function belongs to the local scope of that function.
A variable declared inside a function belongs to the local scope of that function.
Which keyword is used to modify a global variable inside a function?
- A. local
- B. global
- C. nonlocal
- D. static
Answer: B. global
The global keyword allows a function to refer to and modify a variable defined in the global scope.
The global keyword allows a function to refer to and modify a variable defined in the global scope.
Consider the code: x = 5 def outer(): x = 10 def inner(): print(x) inner() outer() What is printed?
- A. 5
- B. 10
- C. Error
- D. None
Answer: B. 10
inner() accesses x from the enclosing function outer(), so it prints 10 according to the LEGB rule.
inner() accesses x from the enclosing function outer(), so it prints 10 according to the LEGB rule.
Fill in the blank: A variable declared inside a function is said to have ______ scope.
- A. global
- B. local
- C. built-in
- D. module
Answer: B. local
Variables created inside a function are accessible only within that function unless declared otherwise.
Variables created inside a function are accessible only within that function unless declared otherwise.