Welcome to the Python Functions Quiz! This quiz is tailored for beginners who are learning about functions in Python. Functions are fundamental to Python programming, allowing for code reusability and better organization. This quiz will help you test and reinforce your understanding of Python functions. Let's begin!
1. What is a function in Python?
Answer:
Explanation:
In Python, a function is a block of code that only executes when it is called. It can receive data, process it, and return a result.
2. How do you define a function in Python?
Answer:
Explanation:
Functions in Python are defined using the 'def' keyword followed by the function name and parentheses.
3. What is the correct syntax to call a function in Python?
Answer:
Explanation:
A function is called by writing the function name followed by parentheses.
4. What are function parameters in Python?
Answer:
Explanation:
Function parameters are values that you can pass to the function when calling it. They act as placeholders for the actual values.
5. What is a return statement in Python?
Answer:
Explanation:
A return statement is used in a function to send the function's result back to the caller.
6. How do you create a function with no parameters in Python?
Answer:
Explanation:
A function with no parameters is defined with empty parentheses.
7. What is the purpose of the 'def' keyword in Python?
Answer:
Explanation:
The 'def' keyword is used to define a new function in Python.
8. What is a default parameter value in a Python function?
Answer:
Explanation:
Default parameter values are specified in the function definition and are used if no corresponding argument is passed during the function call.
9. What will be the output of the following code?
def sum(a, b=2):
return a + b
print(sum(3))
Answer:
Explanation:
The function sum is called with a=3 and uses the default value b=2, resulting in 3+2=5.
10. How do you create a function that returns multiple values in Python?
Answer:
Explanation:
A function can return multiple values as a tuple by separating the values with commas in the return statement.
11. What is a lambda function in Python?
Answer:
Explanation:
Lambda functions are small anonymous functions defined using the lambda keyword.
12. What does the following function definition mean?
def multiply(x, y=1):
return x * y
Answer:
Explanation:
The function multiply has a default parameter y, which means it is optional and defaults to 1 if not provided.
13. What will be the output of the following code?
def check(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
print(check(3))
Answer:
Explanation:
The function check returns "Odd" when the number 3 is passed as it is not divisible by 2.
14. Which of the following is a valid function name in Python?
Answer:
Explanation:
In Python, function names should start with a letter or underscore and can contain letters, numbers, and underscores.
15. What is the purpose of a docstring in a Python function?
Answer:
Explanation:
A docstring is a string literal that occurs as the first statement in a function for documenting the function's purpose and behavior.
16. What is the scope of a variable defined inside a Python function?
Answer:
Explanation:
Variables defined inside a Python function have local scope, meaning they are only accessible within the function.
17. What does the 'global' keyword do in a Python function?
Answer:
Explanation:
The 'global' keyword is used inside a function to refer to a variable defined outside the function, in the global scope.
18. How do you call a function stored in a variable in Python?
Answer:
Explanation:
If a function is assigned to a variable, it can be called using the variable name followed by parentheses.
19. What will be the output of the following code?
def greet():
return "Hello"
print(greet() + " World")
Answer:
Explanation:
The function greet returns "Hello", which is then concatenated with " World" in the print statement.
20. What is a recursive function in Python?
Answer:
Explanation:
A recursive function is one that calls itself within its own definition, often used to solve problems that can be broken down into simpler, similar problems.
Comments
Post a Comment
Leave Comment