Function Definition Syntax
Which keyword is used to define a regular function in Python?
- fun
- def
- lambda
- define
- function
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 -u003E x + 1
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'
Default Parameters
What will be printed by def foo(x=3): print(x) when called as foo()?
- Error
- x
- 3
- foo
- None
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
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
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)
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
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
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]
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*):
Function Documentation
What string immediately after a function definition can describe its purpose?
- Label
- Commentstring
- Docstring
- Refstring
- Funcdesc
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
Function Scope Example
Given x = 10; def foo(): x = 5; print(x), what prints when foo() is called?
- 10
- 15
- Error
- None
- 5
Passing Functions as Arguments
Which is a correct way to pass a function named bar as an argument?
- func[bar]
- func-u003Ebar
- func = bar
- func(bar)
- func(bar())