Python Functions, Lambda Expressions, and Scope Essentials — Questions & Answers

This quiz contains 15 questions. Below is a complete reference of all questions, answer choices, and correct answers. You can use this section to review after taking the interactive quiz above.

  1. Question 1: Function Definition Syntax

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

    • fun
    • def
    • lambda
    • define
    • function
    Show correct answer

    Correct answer: def

  2. Question 2: Lambda Syntax

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

    • def lambda x: x + 1
    • lambda x = x + 1
    • lambda x: x + 1
    • add(x): return x + 1
    • x -> x + 1
    Show correct answer

    Correct answer: lambda x: x + 1

  3. Question 3: Positional Arguments

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

    • greet(name='Alex', 'Smith')
    • greet:[Alex]
    • greet('Alex')
    • greet()
    • greet name='Alex'
    Show correct answer

    Correct answer: greet('Alex')

  4. Question 4: Default Parameters

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

    • Error
    • x
    • 3
    • foo
    • None
    Show correct answer

    Correct answer: 3

  5. Question 5: Return Statement

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

    • return a * b
    • return: a * b
    • send a * b
    • yield a * b
    • output a * b
    Show correct answer

    Correct answer: return a * b

  6. Question 6: Multiple Return Values

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

    • Two separate variables without tuple
    • A tuple containing x and y
    • None
    • A single number
    • An array of x and y
    Show correct answer

    Correct answer: A tuple containing x and y

  7. Question 7: Calling Lambda Functions

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

    • call f(5)
    • invoke f 5
    • f.call(5)
    • lambda f(5)
    • f(5)
    Show correct answer

    Correct answer: f(5)

  8. Question 8: LEGB Rule

    Which describes the LEGB rule for variable scope in Python?

    • Local, Enclosed, Global, Built-in
    • Local, Extend, Global, Built-in
    • Local, Edge, General, Built-in
    • List, Extern, Global, Base
    • Lambda, External, Global, Base
    Show correct answer

    Correct answer: Local, Enclosed, Global, Built-in

  9. Question 9: Global Variable Access

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

    • Use the local variable by default
    • You cannot access it
    • Declare global variable using global keyword
    • Use extern variable
    • Use it without declaration
    Show correct answer

    Correct answer: Declare global variable using global keyword

  10. Question 10: Keyword Arguments

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

    • add(=x3, =y5)
    • add(x:3, y:5)
    • add(y=5, x=3)
    • add(3 5)
    • add[x=3, y=5]
    Show correct answer

    Correct answer: add(y=5, x=3)

  11. Question 11: Variable Number of Arguments

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

    • def func(varargs):
    • def func(*args):
    • def func(...args):
    • def func(args[])
    • def func(args*):
    Show correct answer

    Correct answer: def func(*args):

  12. Question 12: Function Documentation

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

    • Label
    • Commentstring
    • Docstring
    • Refstring
    • Funcdesc
    Show correct answer

    Correct answer: Docstring

  13. Question 13: Anonymous Functions

    Which statement about lambda functions is true?

    • They must return None
    • They are slower than def functions
    • They have a name
    • They use the def keyword
    • They can only have one expression
    Show correct answer

    Correct answer: They can only have one expression

  14. Question 14: Function Scope Example

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

    • 10
    • 15
    • Error
    • None
    • 5
    Show correct answer

    Correct answer: 5

  15. Question 15: Passing Functions as Arguments

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

    • func[bar]
    • func->bar
    • func = bar
    • func(bar)
    • func(bar())
    Show correct answer

    Correct answer: func(bar)