wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Part-A: Python Functions and Scope

Total questions: 30

Worksheet time: 15mins

Name
Class
Date
1.

What is the output? def f(x, y=2, z=3): return x + y*z print(f(2, z=4))

a)

Error

b)

14

c)

10

d)

8

2.

What is printed? def f(a, L=[]): L.append(a) return L print(f(1)) print(f(2))

a)

[1] and [2]

b)

[1] and [1, 2]

c)

[1, 2] and [2]

d)

Error and Error

3.

Which of the following correctly describes *args and **kwargs?

a)

Both must be used together

b)

*args collects extra positional arguments, **kwargs collects keyword arguments

c)

Both store arguments as lists

d)

*args is for keyword arguments, **kwargs for positional

4.

What is the output? def outer(x): def inner(y): return x + y return inner f = outer(10) print(f(5))

a)

Error

b)

15

c)

5

d)

10

5.

What is the output? def f(): x = 10 def g(): nonlocal x x = 20 g() return x print(f())

a)

Error (x not defined)

b)

None

c)

20

d)

10

6.

Which of the following statements is TRUE?

a)

Lambda functions cannot take arguments

b)

A lambda function can contain multiple statements

c)

Lambda functions must return None

d)

A lambda function can contain only one expression

7.

What is the output? def f(x): return lambda y: x*y g = f(3) print(g(4))

a)

3

b)

Error

c)

12

d)

7

8.

What is the output? def fun(n): if n == 0: return 0 return n + fun(n-1) print(fun(4))

a)

10

b)

9

c)

4

d)

8

9.

What does nonlocal do inside nested functions?

a)

Declares a new local variable inside inner function

b)

Refers to a global variable outside all functions

c)

Binds to nearest enclosing function's variable for assignment

d)

Converts variable to a constant at runtime

10.

Which call will raise a TypeError given def f(a, b=1, c=2): pass

a)

f(5, c=3)

b)

f(a=5, b=2)

c)

f(5, 6, 7)

d)

f(a=5, 3)

11.

Consider: def add_end(L=[]): L.append('end') return L Which call sequence shows the shared default list behavior?

a)

add_end([]); add_end([]) -> ['end'], ['end']

b)

add_end(['x']); add_end(['y']) -> ['x','end'], ['y','end']

c)

add_end(); add_end() -> ['end'], ['end', 'end']

d)

add_end(); add_end([]) -> ['end'], ['end']

12.

Given: def make_counter(): count = 0 def inc(): nonlocal count count += 1 return count return inc What is printed by: c = make_counter() print(c()); print(c())

a)

0 and 0

b)

1 and 2

c)

2 and 3

d)

1 and 1

13.

Which statement about closures is correct?

a)

Closures require using class instances with __call__

b)

Closures capture values from enclosing scopes even after return

c)

Closures store only global variables used by inner functions

d)

Closures cannot work with lambda expressions

14.

What is the output? def h(x, L=None): if L is None: L = [] L.append(x) return L print(h(1)); print(h(2))

a)

[1] and [2]

b)

[] and []

c)

[1] and [1, 2]

d)

[1, 2] and [2]

15.

How many times is the function fun called (including the base call)? def fun(n): if n <= 0: return fun(n-1) fun(5)

a)

5

b)

10

c)

4

d)

6

16.

What is printed by this program? def f(n): if n == 0: return print(n, end=" ") f(n-1) print(n, end=" ") f(3)

a)

3 2 1 2 3

b)

3 2 1 1 2 3

c)

1 2 3 3 2 1

d)

3 2 1

17.

Given the function below, what will be printed? def foo(n): if n == 1: return 1 return foo(n-1) + foo(n-1) print(foo(4))

a)

4

b)

8

c)

16

d)

2

18.

What is the time complexity of the recursive function foo(n) defined as: foo(n) = 2 * foo(n-1), with foo(1) = 1

a)

O(2n)O(2^n)

b)

O(n2)O(n^2)

c)

O(n log n)

d)

O(n)

19.

What happens if a recursive function does not have a base case?

a)

Python automatically stops it immediately

b)

It executes only once

c)

It returns None

d)

It causes infinite recursion and finally raises an error

20.

What is the output of this program? def f(n): if n <= 1: return 1 return n * f(n-2) print(f(5))

a)

15

b)

8

c)

5

d)

10

21.

Which statement is TRUE about recursion in Python?

a)

Python optimizes tail recursion automatically

b)

Every recursive function is faster than iteration

c)

Recursion cannot return values

d)

Recursive calls use the call stack

22.

Which line defines the base case in the function below? def countdown(n): if n == 0: return "Done" return countdown(n-1)

a)

def countdown(n):

b)

return "Done"

c)

if n == 0:

d)

return countdown(n-1)

23.

What will this function print? def trace(n): if n == 0: return trace(n-1) print(n, end=" ") trace(3)

a)

3 1 2

b)

2 3 1

c)

1 2 3

d)

3 2 1

24.

Which stack behavior best describes how recursive calls are handled at runtime?

a)

First-In First-Out order

b)

Last-In First-Out order

c)

Random access order

d)

Parallel execution order

25.

Consider: def g(n): if n <= 0: return 0 return 1 + g(n-1) What does g(4) return?

a)

0

b)

3

c)

4

d)

5

26.

Which change prevents infinite recursion in this function? def h(n): return h(n+1)

a)

Increase recursion limit setting

b)

Replace recursion with a loop only

c)

Call h(n-1) without returning

d)

Add a base case when n == 0

27.

Given: def pal(s): if len(s) <= 1: return True if s[0] != s[-1]: return False return pal(s[1:-1]) What does pal("level") return?

a)

Error

b)

False

c)

True

d)

None

28.

For the recurrence T(n) = T(n-1) + c with T(1) = c, the time complexity is

a)

O(1)

b)

O(log n)

c)

O(n)

d)

O(n2)O(n^2)

29.

Which best explains why a missing base case can lead to RecursionError in Python?

a)

The call stack grows until depth limit is reached

b)

The interpreter forbids nested functions

c)

The return statements are type mismatched

d)

Python converts recursion to iteration automatically

30.

What is printed? def zig(n): if n == 0: return print("pre", n) zig(n-1) print("post", n) zig(2)

a)

pre 2 pre 1 post 1 post 2

b)

pre 1 pre 2 post 2 post 1

c)

pre 2 post 2 pre 1 post 1

d)

pre 1 post 1 pre 2 post 2