Functions in Python are blocks of code that are defined with the <em>def</em>
keyword. They are executed only when they are called. Functions can accept input data known as parameters or arguments, and they can also return data as a result. This allows you to encapsulate and reuse code, making your programs more organized and efficient. Here we present 15 multiple-choice questions to test your knowledge of Python Functions. Each MCQ has the correct answer with an explanation.
1. How is a function defined in Python?
Answer:
Explanation:
In Python, a function is defined using the 'def' keyword followed by the function name and parentheses.
2. How do you call a function named 'myFunction' in Python?
Answer:
Explanation:
A function is called by writing its name followed by parentheses.
3. What is the correct way to define a function with one parameter 'x' in Python?
Answer:
Explanation:
Function parameters are specified within the parentheses in the function definition.
4. How do you return a value from a function in Python?
Answer:
Explanation:
The 'return' statement is used to return a value from a function.
5. What is a default parameter in Python functions?
Answer:
Explanation:
Default parameters are those that have a default value and can be omitted when calling the function.
6. How do you create a function with variable number of arguments in Python?
Answer:
Explanation:
The *args syntax is used to pass a variable number of arguments to a function.
7. What does a lambda function in Python do?
Answer:
Explanation:
Lambda functions are small anonymous functions defined using the lambda keyword.
8. How do you specify a docstring in a Python function?
Answer:
Explanation:
Docstrings are specified using triple quotes at the beginning of a function.
9. What is the output of the following code?
Answer:
Explanation:
The pass statement in a function does nothing, and a function without a return statement returns None by default.
10. How do you define a function that takes an unlimited number of keyword arguments?
Answer:
Explanation:
**kwargs allows passing a variable number of keyword arguments to a function.
11. What is recursion in Python?
Answer:
Explanation:
Recursion occurs when a function calls itself.
12. How do you make a variable defined inside a function accessible outside the function?
Answer:
Explanation:
The global keyword allows a variable defined inside a function to be accessible globally.
13. What is an anonymous function in Python?
Answer:
Explanation:
Anonymous functions, also known as lambda functions, are defined without a name.
14. What is a function decorator in Python?
Answer:
Explanation:
Decorators are used in Python to modify or extend the behavior of functions or methods.
15. What does the 'yield' keyword do in Python?
Answer:
Explanation:
The 'yield' keyword is used in generator functions to pause the function execution and send a value back to the caller, but retains enough state to enable the function to resume where it left off.