Python Functions, Lambda Expressions, and Scope Essentials Quiz

  1. Function Definition Syntax

    Which keyword is used to define a regular function in Python?

    1. fun
    2. def
    3. lambda
    4. define
    5. function
  2. Lambda Syntax

    What is the correct way to write a lambda function that adds 1 to its input x?

    1. def lambda x: x + 1
    2. lambda x = x + 1
    3. lambda x: x + 1
    4. add(x): return x + 1
    5. x -u003E x + 1
  3. Positional Arguments

    Given def greet(name):, which call provides a valid positional argument?

    1. greet(name='Alex', 'Smith')
    2. greet:[Alex]
    3. greet('Alex')
    4. greet()
    5. greet name='Alex'
  4. Default Parameters

    What will be printed by def foo(x=3): print(x) when called as foo()?

    1. Error
    2. x
    3. 3
    4. foo
    5. None
  5. Return Statement

    Which statement correctly returns the product of two numbers a and b?

    1. return a * b
    2. return: a * b
    3. send a * b
    4. yield a * b
    5. output a * b
  6. Multiple Return Values

    If a function returns x, y as return x, y, what does the caller receive?

    1. Two separate variables without tuple
    2. A tuple containing x and y
    3. None
    4. A single number
    5. An array of x and y
  7. Calling Lambda Functions

    How do you call a lambda assigned to f for input 5 (f = lambda n: n * n)?

    1. call f(5)
    2. invoke f 5
    3. f.call(5)
    4. lambda f(5)
    5. f(5)
  8. LEGB Rule

    Which describes the LEGB rule for variable scope in Python?

    1. Local, Enclosed, Global, Built-in
    2. Local, Extend, Global, Built-in
    3. Local, Edge, General, Built-in
    4. List, Extern, Global, Base
    5. Lambda, External, Global, Base
  9. Global Variable Access

    How do you access a global variable inside a function if a variable with the same name exists locally?

    1. Use the local variable by default
    2. You cannot access it
    3. Declare global variable using global keyword
    4. Use extern variable
    5. Use it without declaration
  10. Keyword Arguments

    Which call correctly uses keyword arguments for def add(x, y=2)?

    1. add(=x3, =y5)
    2. add(x:3, y:5)
    3. add(y=5, x=3)
    4. add(3 5)
    5. add[x=3, y=5]
  11. Variable Number of Arguments

    Which syntax allows a function to accept any number of positional arguments?

    1. def func(varargs):
    2. def func(*args):
    3. def func(...args):
    4. def func(args[])
    5. def func(args*):
  12. Function Documentation

    What string immediately after a function definition can describe its purpose?

    1. Label
    2. Commentstring
    3. Docstring
    4. Refstring
    5. Funcdesc
  13. Anonymous Functions

    Which statement about lambda functions is true?

    1. They must return None
    2. They are slower than def functions
    3. They have a name
    4. They use the def keyword
    5. They can only have one expression
  14. Function Scope Example

    Given x = 10; def foo(): x = 5; print(x), what prints when foo() is called?

    1. 10
    2. 15
    3. Error
    4. None
    5. 5
  15. Passing Functions as Arguments

    Which is a correct way to pass a function named bar as an argument?

    1. func[bar]
    2. func-u003Ebar
    3. func = bar
    4. func(bar)
    5. func(bar())