This quiz rigorously tests your knowledge of Python functions, lambda expressions, argument handling, default values, the LEGB scoping rule, and related advanced features. Demonstrate your mastery of function behavior and Python's execution model.
Identifying Keyword-Only Arguments
Given the function definition 'def func(a, *, b=3):', which of the following is a correct way to call this function?
- func(2, 4, 5)
- func(2, 4)
- func(b=4, 2)
- func(a=2, 3)
- func(a=2, b=4)
Understanding Lambda Return Values
What will be the result of executing 'add = lambda x, y=3: x+y; add(2)'?
- add2
- TypeError
- 5
- None
- 6
Modifying a List in a Function
If you have 'def f(lst=[]): lst.append(1); return lst', what will 'f(); f();' return on its second call?
- []
- [1]
- [1, 1]
- [2]
- None
Order of Evaluation for Default Arguments
In 'def f(a, b=a+1):', why will this definition result in an error?
- Because 'b' must come before 'a'
- Because default arguments are evaluated at function definition time
- Because default arguments must be constants
- Because default arguments are evaluated at function call time
- Because 'a' cannot be referenced at all
LEGB Rule in Local vs. Global
Given 'x = 10; def foo(): x = 20; return x; print(foo())', what is printed?
- None
- 20
- 30
- UnboundLocalError
- 10
Scope with Nested Functions
If you have 'x = 1; def outer(): x = 2; def inner(): return x; return inner();', what does 'outer()' return?
- None
- 2
- Error
- 1
- 0
Global Keyword Impact
What is the effect of using 'global x' inside a function in Python?
- It declares 'x' as referring to the global variable
- It creates a new local variable 'x'
- It errors if 'x' does not exist globally
- It restricts 'x' from being reassigned
- It causes 'x' to be constant
Anonymous Function Syntax
Which of the following declares an equivalent lambda to 'def square(x): return x * x'?
- lambda x: x**
- lambda x: x*x
- lambda x: return x*x
- lambda x =u003E x*x
- lambda: x*x
Mutable Default Arguments
Why is using a mutable object like a list as a function's default value often problematic in Python?
- The same object is shared across function calls
- Lists can't be passed as arguments
- Default arguments must be immutable
- Each call creates a new list
- Python does not allow default parameters
Closures Capturing Variables
Given 'def make_adder(x): return lambda y: x + y', what does 'make_adder(5)(10)' return?
- 10
- TypeError
- make_adder
- 510
- 15
Return Statement Behavior
What is the return value of 'def foo(): pass' when called as 'foo()'?
- foo
- 0
- None
- ''
- Error
Nonlocal Keyword Scope
When would you use the 'nonlocal' statement in Python?
- To modify a variable in an enclosing function's scope
- To import non-local modules
- To refer to a global variable
- To declare a static variable
- To hide a local variable
Arguments Unpacking
Given 'def f(a, b, c): return a + b + c', which call is valid if 'args = (1, 2, 3)'?
- f(*args)
- f(*args, *args)
- f(args)
- f(args=1,2,3)
- f(**args)
Keyword Arguments in Lambdas
Which lambda expression accepts two keyword arguments 'a' and 'b' and returns their sum?
- lambda: a + b
- lambda a, b: a + b
- lambda a+b: a+b
- lambda *a, b: b + a
- lambda (a, b): a + b
Function Annotations
What is the value of 'add.__annotations__' after 'def add(a: int, b: int) -u003E int: return a + b'?
- TypeError
- None
- ['a', 'b', 'return']
- {int: 'a', int: 'b', int: 'return'}
- {'a': int, 'b': int, 'return': int}