NEW
Font size
WorksheetsPart-A: Python Functions and Scope
Total questions: 30
Worksheet time: 15mins
What is the output? def f(x, y=2, z=3): return x + y*z print(f(2, z=4))
Error
14
10
8
What is printed? def f(a, L=[]): L.append(a) return L print(f(1)) print(f(2))
[1] and [2]
[1] and [1, 2]
[1, 2] and [2]
Error and Error
Which of the following correctly describes *args and **kwargs?
Both must be used together
*args collects extra positional arguments, **kwargs collects keyword arguments
Both store arguments as lists
*args is for keyword arguments, **kwargs for positional
What is the output? def outer(x): def inner(y): return x + y return inner f = outer(10) print(f(5))
Error
15
5
10
What is the output? def f(): x = 10 def g(): nonlocal x x = 20 g() return x print(f())
Error (x not defined)
None
20
10
Which of the following statements is TRUE?
Lambda functions cannot take arguments
A lambda function can contain multiple statements
Lambda functions must return None
A lambda function can contain only one expression
What is the output? def f(x): return lambda y: x*y g = f(3) print(g(4))
3
Error
12
7
What is the output? def fun(n): if n == 0: return 0 return n + fun(n-1) print(fun(4))
10
9
4
8
What does nonlocal do inside nested functions?
Declares a new local variable inside inner function
Refers to a global variable outside all functions
Binds to nearest enclosing function's variable for assignment
Converts variable to a constant at runtime
Which call will raise a TypeError given def f(a, b=1, c=2): pass
f(5, c=3)
f(a=5, b=2)
f(5, 6, 7)
f(a=5, 3)
Consider: def add_end(L=[]): L.append('end') return L Which call sequence shows the shared default list behavior?
add_end([]); add_end([]) -> ['end'], ['end']
add_end(['x']); add_end(['y']) -> ['x','end'], ['y','end']
add_end(); add_end() -> ['end'], ['end', 'end']
add_end(); add_end([]) -> ['end'], ['end']
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())
0 and 0
1 and 2
2 and 3
1 and 1
Which statement about closures is correct?
Closures require using class instances with __call__
Closures capture values from enclosing scopes even after return
Closures store only global variables used by inner functions
Closures cannot work with lambda expressions
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))
[1] and [2]
[] and []
[1] and [1, 2]
[1, 2] and [2]
How many times is the function fun called (including the base call)? def fun(n): if n <= 0: return fun(n-1) fun(5)
5
10
4
6
What is printed by this program? def f(n): if n == 0: return print(n, end=" ") f(n-1) print(n, end=" ") f(3)
3 2 1 2 3
3 2 1 1 2 3
1 2 3 3 2 1
3 2 1
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))
4
8
16
2
What is the time complexity of the recursive function foo(n) defined as: foo(n) = 2 * foo(n-1), with foo(1) = 1
O(2n)
O(n2)
O(n log n)
O(n)
What happens if a recursive function does not have a base case?
Python automatically stops it immediately
It executes only once
It returns None
It causes infinite recursion and finally raises an error
What is the output of this program? def f(n): if n <= 1: return 1 return n * f(n-2) print(f(5))
15
8
5
10
Which statement is TRUE about recursion in Python?
Python optimizes tail recursion automatically
Every recursive function is faster than iteration
Recursion cannot return values
Recursive calls use the call stack
Which line defines the base case in the function below? def countdown(n): if n == 0: return "Done" return countdown(n-1)
def countdown(n):
return "Done"
if n == 0:
return countdown(n-1)
What will this function print? def trace(n): if n == 0: return trace(n-1) print(n, end=" ") trace(3)
3 1 2
2 3 1
1 2 3
3 2 1
Which stack behavior best describes how recursive calls are handled at runtime?
First-In First-Out order
Last-In First-Out order
Random access order
Parallel execution order
Consider: def g(n): if n <= 0: return 0 return 1 + g(n-1) What does g(4) return?
0
3
4
5
Which change prevents infinite recursion in this function? def h(n): return h(n+1)
Increase recursion limit setting
Replace recursion with a loop only
Call h(n-1) without returning
Add a base case when n == 0
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?
Error
False
True
None
For the recurrence T(n) = T(n-1) + c with T(1) = c, the time complexity is
O(1)
O(log n)
O(n)
O(n2)
Which best explains why a missing base case can lead to RecursionError in Python?
The call stack grows until depth limit is reached
The interpreter forbids nested functions
The return statements are type mismatched
Python converts recursion to iteration automatically
What is printed? def zig(n): if n == 0: return print("pre", n) zig(n-1) print("post", n) zig(2)
pre 2 pre 1 post 1 post 2
pre 1 pre 2 post 2 post 1
pre 2 post 2 pre 1 post 1
pre 1 post 1 pre 2 post 2
