Python Functions Quiz - MCQ Questions and Answers

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?

a) A block of code that only runs when it is called
b) A variable used to store data
c) A way to print output to the console
d) An error message

Answer:

a) A block of code that only runs when it is called

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?

a) function myFunction():
b) def myFunction():
c) create myFunction():
d) new myFunction():

Answer:

b) def myFunction():

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?

a) call myFunction()
b) myFunction()
c) execute myFunction()
d) run myFunction()

Answer:

b) myFunction()

Explanation:

A function is called by writing the function name followed by parentheses.

4. What are function parameters in Python?

a) Variables inside functions
b) Errors in functions
c) Values passed to functions
d) Return values of functions

Answer:

c) Values passed to functions

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?

a) A way to exit a program
b) A statement that ends a function
c) A statement to print a value
d) A statement that sends a result back to the caller

Answer:

d) A statement that sends a result back to the caller

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?

a) def myFunction():
b) def myFunction(None):
c) def myFunction(0):
d) def myFunction(empty):

Answer:

a) def myFunction():

Explanation:

A function with no parameters is defined with empty parentheses.

7. What is the purpose of the 'def' keyword in Python?

a) It indicates the start of a loop
b) It defines a new variable
c) It defines a new function
d) It imports a module

Answer:

c) It defines a new function

Explanation:

The 'def' keyword is used to define a new function in Python.

8. What is a default parameter value in a Python function?

a) A value that is optional when calling the function
b) The first value passed to the function
c) A constant value in a function
d) A value that is used if no argument is provided

Answer:

d) A value that is used if no argument is provided

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))
a) 3
b) 5
c) 6
d) Error

Answer:

b) 5

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?

a) Using multiple return statements
b) By returning a tuple
c) By separating return values with a comma
d) This is not possible in Python

Answer:

b) By returning a tuple

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?

a) A long, complex function
b) A function that is defined without a name
c) A function that can only be used once
d) A function that returns lambda

Answer:

b) A function that is defined without a name

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
a) The function requires two arguments
b) The function will only multiply x
c) y is an optional parameter with a default value of 1
d) The function will not work without specifying y

Answer:

c) y is an optional parameter with a default value of 1

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))
a) Even
b) Odd
c) Error
d) None

Answer:

b) Odd

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?

a) 1_function
b) function_name
c) function-name
d) function.name

Answer:

b) function_name

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?

a) To document the purpose and usage of the function
b) To declare function parameters
c) To improve the speed of the function
d) To declare the return type

Answer:

a) To document the purpose and usage of the function

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?

a) Global
b) Local
c) External
d) Universal

Answer:

b) Local

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?

a) It creates a new global variable
b) It imports a global module
c) It makes a local variable accessible globally
d) It deletes a global variable

Answer:

c) It makes a local variable accessible globally

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?

a) functionName()
b) call functionName
c) variableName()
d) execute variableName

Answer:

c) variableName()

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")
a) Hello
b) World
c) HelloWorld
d) Hello World

Answer:

d) Hello World

Explanation:

The function greet returns "Hello", which is then concatenated with " World" in the print statement.

20. What is a recursive function in Python?

a) A function that calls another function
b) A function that calls itself
c) A function that runs indefinitely
d) A function that never ends

Answer:

b) A function that calls itself

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