Python: Functions, Lambda u0026 Scope Challenge Quiz

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.

  1. 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?

    1. func(2, 4, 5)
    2. func(2, 4)
    3. func(b=4, 2)
    4. func(a=2, 3)
    5. func(a=2, b=4)
  2. Understanding Lambda Return Values

    What will be the result of executing 'add = lambda x, y=3: x+y; add(2)'?

    1. add2
    2. TypeError
    3. 5
    4. None
    5. 6
  3. 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. []
    2. [1]
    3. [1, 1]
    4. [2]
    5. None
  4. Order of Evaluation for Default Arguments

    In 'def f(a, b=a+1):', why will this definition result in an error?

    1. Because 'b' must come before 'a'
    2. Because default arguments are evaluated at function definition time
    3. Because default arguments must be constants
    4. Because default arguments are evaluated at function call time
    5. Because 'a' cannot be referenced at all
  5. LEGB Rule in Local vs. Global

    Given 'x = 10; def foo(): x = 20; return x; print(foo())', what is printed?

    1. None
    2. 20
    3. 30
    4. UnboundLocalError
    5. 10
  6. Scope with Nested Functions

    If you have 'x = 1; def outer(): x = 2; def inner(): return x; return inner();', what does 'outer()' return?

    1. None
    2. 2
    3. Error
    4. 1
    5. 0
  7. Global Keyword Impact

    What is the effect of using 'global x' inside a function in Python?

    1. It declares 'x' as referring to the global variable
    2. It creates a new local variable 'x'
    3. It errors if 'x' does not exist globally
    4. It restricts 'x' from being reassigned
    5. It causes 'x' to be constant
  8. Anonymous Function Syntax

    Which of the following declares an equivalent lambda to 'def square(x): return x * x'?

    1. lambda x: x**
    2. lambda x: x*x
    3. lambda x: return x*x
    4. lambda x =u003E x*x
    5. lambda: x*x
  9. Mutable Default Arguments

    Why is using a mutable object like a list as a function's default value often problematic in Python?

    1. The same object is shared across function calls
    2. Lists can't be passed as arguments
    3. Default arguments must be immutable
    4. Each call creates a new list
    5. Python does not allow default parameters
  10. Closures Capturing Variables

    Given 'def make_adder(x): return lambda y: x + y', what does 'make_adder(5)(10)' return?

    1. 10
    2. TypeError
    3. make_adder
    4. 510
    5. 15
  11. Return Statement Behavior

    What is the return value of 'def foo(): pass' when called as 'foo()'?

    1. foo
    2. 0
    3. None
    4. ''
    5. Error
  12. Nonlocal Keyword Scope

    When would you use the 'nonlocal' statement in Python?

    1. To modify a variable in an enclosing function's scope
    2. To import non-local modules
    3. To refer to a global variable
    4. To declare a static variable
    5. To hide a local variable
  13. Arguments Unpacking

    Given 'def f(a, b, c): return a + b + c', which call is valid if 'args = (1, 2, 3)'?

    1. f(*args)
    2. f(*args, *args)
    3. f(args)
    4. f(args=1,2,3)
    5. f(**args)
  14. Keyword Arguments in Lambdas

    Which lambda expression accepts two keyword arguments 'a' and 'b' and returns their sum?

    1. lambda: a + b
    2. lambda a, b: a + b
    3. lambda a+b: a+b
    4. lambda *a, b: b + a
    5. lambda (a, b): a + b
  15. Function Annotations

    What is the value of 'add.__annotations__' after 'def add(a: int, b: int) -u003E int: return a + b'?

    1. TypeError
    2. None
    3. ['a', 'b', 'return']
    4. {int: 'a', int: 'b', int: 'return'}
    5. {'a': int, 'b': int, 'return': int}