This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Function”.
1. Which of the following is the use of function in python?
a) Functions are reusable pieces of programs
b) Functions don’t provide better modularity for your application
c) you can’t also create your own functions
d) All of the mentioned
View Answer
Explanation: Functions in Python are reusable blocks of code that perform a specific task. They improve modularity, make code easier to read and maintain, and help avoid repetition. You can also define your own functions using the def keyword.
2. Which keyword is used for function?
a) Fun
b) Define
c) def
d) Function
View Answer
Explanation: The def keyword is used in Python to define a function. It is followed by the function name and parentheses that may include parameters. For example: def my_function():.
3. What will be the output of the following Python code?
def sayHello(): print('Hello World!') sayHello() sayHello()
a)
Hello World! Hello World!
b)
'Hello World!' 'Hello World!'
c)
Hello Hello
d) None of the mentioned
View Answer
Explanation: The function sayHello() prints “Hello World!” each time it’s called. Since it is called twice, the output is printed twice on separate lines. Functions help avoid code repetition.
4. What will be the output of the following Python code?
def printMax(a, b): if a > b: print(a, 'is maximum') elif a == b: print(a, 'is equal to', b) else: print(b, 'is maximum') printMax(3, 4)
a) 3
b) 4
c) 4 is maximum
d) None of the mentioned
View Answer
Explanation: The function printMax(a, b) compares two numbers and prints which one is greater or if they are equal. In the call printMax(3, 4), since 4 is greater than 3, the else block is executed and it prints 4 is maximum.
5. What will be the output of the following Python code?
x = 50 def func(x): print('x is', x) x = 2 print('Changed local x to', x) func(x) print('x is now', x)
a)
x is 50 Changed local x to 2 x is now 50
b)
x is 50 Changed local x to 2 x is now 2
c)
x is 50 Changed local x to 2 x is now 100
d) None of the mentioned
View Answer
Explanation: The function func(x) takes a parameter x and prints its value. Inside the function, the value of x is reassigned to 2, which is a local change and does not affect the global variable x. So, when func(x) is called, it prints x is 50 followed by Changed local x to 2. Outside the function, the global x is still 50, so print(‘x is now’, x) outputs x is now 50.
6. What will be the output of the following Python code?
x = 50 def func(): global x print('x is', x) x = 2 print('Changed global x to', x) func() print('Value of x is', x)
a)
x is 50 Changed global x to 2 Value of x is 50
b)
x is 50 Changed global x to 2 Value of x is 2
c)
x is 50 Changed global x to 50 Value of x is 50
d) None of the mentioned
View Answer
Explanation: The global keyword is used to indicate that x inside the function refers to the global variable. So, when x = 2 is executed inside func(), it modifies the global x. Hence, after calling the function, the global variable x becomes 2.
7. What will be the output of the following Python code?
def say(message, times = 1): print(message * times) say('Hello') say('World', 5)
a)
Hello WorldWorldWorldWorldWorld
b)
Hello World 5
c)
Hello World,World,World,World,World
d)
Hello HelloHelloHelloHelloHelloView Answer
Explanation: The function say() takes two parameters: message & times, with times defaulting to 1.
- say(‘Hello’) prints “Hello” once because times defaults to 1.
- say(‘World’, 5) prints “World” five times, concatenated together (i.e., WorldWorldWorldWorldWorld).
This demonstrates Python’s default arguments feature, allowing functions to have optional parameters.
8. What will be the output of the following Python code?
def func(a, b=5, c=10): print('a is', a, 'and b is', b, 'and c is', c) func(3, 7) func(25, c = 24) func(c = 50, a = 100)
a)
a is 7 and b is 3 and c is 10 a is 25 and b is 5 and c is 24 a is 5 and b is 100 and c is 50
b)
a is 3 and b is 7 and c is 10 a is 5 and b is 25 and c is 24 a is 50 and b is 100 and c is 5
c)
a is 3 and b is 7 and c is 10 a is 25 and b is 5 and c is 24 a is 100 and b is 5 and c is 50
d) None of the mentioned
View Answer
Explanation: Here,
- func(3, 7) assigns a=3, b=7, and uses default c=10.
- func(25, c=24) assigns a=25 (positional), c=24 (keyword), and default b=5.
- func(c=50, a=100) uses keyword arguments, so a=100, c=50, and default b=5.
This shows how keyword arguments let you specify parameters out of order and selectively override defaults.
9. What will be the output of the following Python code?
def maximum(x, y): if x > y: return x elif x == y: return 'The numbers are equal' else: return y print(maximum(2, 3))
a) 2
b) 3
c) The numbers are equal
d) None of the mentioned
View Answer
Explanation: The function maximum(x, y) compares two numbers. Since 2 < 3, it goes to the else block and returns y, which is 3.
10. Which of the following is a feature of DocString?
a) Provide a convenient way of associating documentation with Python modules, functions, classes, and methods
b) All functions should have a docstring
c) Docstrings can be accessed by the __doc__ attribute on objects
d) All of the mentioned
View Answer
Explanation: DocStrings provide built-in documentation for Python modules, functions, classes, and methods. They are accessed via the __doc__ attribute and it’s recommended that all functions have them for better code clarity and maintainability. Hence, all the mentioned points are true.
More MCQs on Python Function:
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Information Technology Books
- Apply for Python Internship
- Apply for Programming Internship
- Check Python Books
- Practice Programming MCQs