Exercise: Python

Questions for: Functions

How do you pass a variable number of keyword arguments to a function?
A:
Using the **kwargs syntax
B:
Using the *args syntax
C:
Using the varkwargs() function
D:
Using the kwarg_count parameter
Answer: A
The **kwargs syntax allows a function to accept a variable number of keyword arguments.
What will be the result of the following code snippet?
def power(x, n=2):
    return x ** n

result = power(3)
print(result)
A:
6
B:
9
C:
3
D:
12
Answer: B
The function power calculates the square of the given argument x by default, and in this case, it squares 3, resulting in 9.
What is the purpose of the nonlocal keyword?
A:
Declares a local variable
B:
Specifies the scope of a function
C:
Converts a global variable to a local variable
D:
Refers to a variable in the nearest enclosing scope
Answer: D
The nonlocal keyword is used to indicate that a variable refers to the nearest enclosing scope that is not global.
What does the term "recursion" refer to in the context of programming?
A:
Declaring a variable again
B:
A function calling itself
C:
Reassigning the value of a variable
D:
Using a loop construct
Answer: B
Recursion is a programming concept where a function calls itself in its own definition.
What is the purpose of the lambda keyword?
A:
Declares a variable
B:
Defines a class
C:
Creates an anonymous function
D:
Specifies the return type of a function
Answer: C
The lambda keyword is used to create anonymous functions, also known as lambda functions.
Ad Slot (Above Pagination)
Quiz