wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Quiz4-CC102-MakScie

Total questions: 40

Worksheet time: 40mins

Name
Class
Date
1.

Which of the following best describes “scope” in Python?

a)

The part of code where a variable is visible

b)

The lifetime of a variable

c)

The type of variable

d)

The memory location of a variable

2.

Which scope applies to variables defined inside a function?

a)

Global

b)

Local

c)

Enclosing

d)

Built-in

3.

Variables declared inside a function are destroyed when the function exits. This refers to:

a)

Scope

b)

Lifetime

c)

Instantiation

d)

Memory mapping

4.

Which keyword allows modification of a global variable inside a function?

a)

nonlocal

b)

global

c)

static

d)

extern

5.

Which of the following is NOT part of the LEGB rule?

a)

nonlocal

b)

global

c)

static

d)

extern

6.

What does the “E” in LEGB stand for?

a)

Environment

b)

External

c)

Enclosing

d)

Execution

7.

Built-in scope contains:

a)

User-defined global variables

b)

Python keywords and functions

c)

Environment variables

d)

Variables imported from other modules

8.

What happens if Python cannot find a variable in any LEGB scope?

a)

It raises SyntaxError

b)

It assigns a default value

c)

It raises NameError

d)

It generates a warning

9.

What will be the output?

x = 10

def test():

    x = 20

test()

print(x)

a)

The value of x remains 10 outside the function.

b)

The value of x changes to 20 outside the function.

c)

The value of x is undefined outside the function.

d)

An error occurs due to variable scope.

10.

What will be the output?

x = 5

def sample():

    global x

    x = 15

sample()

print(x)

a)

A. 5

b)

B. 10

c)

C. 15

d)

D. Error

11.

What keyword is used to modify a variable from an outer (non-global) function?

a)

global

b)

outer

c)

nonlocal

d)

static

12.

What is the output?

def outer():

    x = 5

    def inner():

        print(x)

    inner()

outer()

a)

Error

b)

5

c)

None

d)

Undefined

13.

Storage class in Python refers mainly to:

a)

How long a variable exists

b)

A variable’s type

c)

Memory management syntax

d)

External library storage

14.

Python variables are stored:

a)

On the stack only

b)

On the heap only

c)

On both stack and heap

d)

In the CPU register

15.

Which variable’s lifetime lasts until the program ends?

a)

Local

b)

Global

c)

Enclosing

d)

Built-in

16.

What will this code print?

def func():

    x = 2

print(x)

a)

A. 2

b)

B. None

c)

C. Error

d)

D. Undefined

17.

A function’s local variables are stored in:

a)

Global namespace

b)

Local namespace

c)

Built-in namespace

d)

Class namespace

18.

Which is TRUE about Python namespaces?

a)

They are dictionaries

b)

They are lists

c)

They are sets

d)

They are static memory blocks

19.

The built-in function that returns current namespace as a dictionary is:

a)

scope()

b)

globals()

c)

namespace()

d)

builtins()

20.

The local namespace of a function can be inspected using:

a)

locals()

b)

globals()

c)

scope()

d)

inspect()

21.

Which of the following refers to a variable defined in a class but outside any method?

a)

Local variable

b)

Class variable

c)

Instance variable

d)

Global variable

22.

Which variable is unique for each object instance?

a)

Class variable

b)

Static variable

c)

Instance variable

d)

Global variable

23.

What is the output?

x = 1

def f():

    x = x + 1

    print(x)

f()

a)

2

b)

1

c)

Error

d)

None

24.

A global variable can be accessed inside a function without using global keyword if:

a)

It is not modified

b)

It is a constant

c)

It is integer

d)

It is declared first

25.

What do you call a variable first created inside a function but declared global?

a)

Local variable

b)

Enclosing variable

c)

Global variable

d)

Dynamic variable

26.

In nested functions, the nonlocal variable refers to:

a)

Global variable

b)

Local variable of inner function

c)

Local variable of outer function

d)

Built-in variable

27.

What is the output?

def outer():

    x = 3

    def inner():

        nonlocal x

        x = 5

    inner()

    print(x)

outer()

a)

A. 3

b)

B. 5

c)

C. Error

d)

D. None

28.

Which storage characteristic does Python NOT explicitly support like C?

a)

automatic

b)

register

c)

static

d)

All of the above

29.

Global variables declared inside a function using the global keyword belong to:

a)

Local namespace

b)

Enclosing namespace

c)

Global namespace

d)

Built-in namespace

30.

Which statement is TRUE?

a)

Global variables can only be declared at the top of the program

b)

Global variables persist until program termination

c)

Local variables persist until program termination

d)

Nonlocal variables persist permanently

31.

What is shadowing?

a)

Using global inside local

b)

Identical names in different scopes

c)

Using nonlocal on global variables

d)

Removing variable reference

32.

What is the output?

x = 10

def f():

    print(x)

def g():

    x = 5

    f()

g()

a)

10

b)

5

c)

Error

d)

None

33.

Which of the following is the default namespace in Python?

a)

builtins module

b)

user namespace

c)

global namespace

d)

static memory

34.

What type of variable has the shortest lifetime?

a)

Class variable

b)

Global variable

c)

Local variable

d)

Built-in variable

35.

What will this print?

def func(a):

    b = a + 2

    return b

print(b)

a)

A. 2

b)

B. 3

c)

C. Error

d)

D. Undefined

36.

The lifetime of a local variable begins when:

a)

Program starts

b)

Function is called

c)

Module loads

d)

Class is created

37.

The LEGB rule resolves names in what order?

a)

Local → Built-in → Enclosing → Global

b)

Local → Enclosing → Global → Built-in

c)

Global → Local → Enclosing → Built-in

d)

Built-in → Global → Local → Enclosing

38.

What is the output?

count = 0

def increment():

    global count

    count += 1

increment()

print(count

a)

A. 0

b)

B. 1

c)

C. Error

d)

D. None

39.

What will happen if a variable is accessed before assignment in local scope?

a)

It prints None

b)

It uses global value

c)

NameError

d)

SyntaxError

40.

Which of the following is the correct way to list all built-in functions in Python?

a)

builtins()

b)

dir(builtins)

c)

builtins.list()

d)

namespace()