Python Programming - Functions

Exercise : Functions - General Questions
  • Functions - General Questions
11.
What is the purpose of the nonlocal keyword?
Declares a local variable
Specifies the scope of a function
Converts a global variable to a local variable
Refers to a variable in the nearest enclosing scope
Answer: Option
Explanation:
The nonlocal keyword is used to indicate that a variable refers to the nearest enclosing scope that is not global.

12.
What will be the result of the following code snippet?
def power(x, n=2):
    return x ** n

result = power(3)
print(result)
6
9
3
12
Answer: Option
Explanation:
The function power calculates the square of the given argument x by default, and in this case, it squares 3, resulting in 9.

13.
How do you pass a variable number of keyword arguments to a function?
Using the **kwargs syntax
Using the *args syntax
Using the varkwargs() function
Using the kwarg_count parameter
Answer: Option
Explanation:
The **kwargs syntax allows a function to accept a variable number of keyword arguments.

14.
What is the purpose of the map() function?
Iterates over elements of a list
Applies a function to each item in an iterable
Checks if a variable is of a certain type
Creates a new list with specified values
Answer: Option
Explanation:
The map() function applies a given function to each item in an iterable (e.g., a list).

15.
What does the term "docstring" refer to?
A variable that stores documentation
A string containing documentation for a function or module
A string used for debugging purposes
A reserved keyword in Python
Answer: Option
Explanation:
A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition and is used for documentation.