05 Functions
05 Functions
MCQ
Q. Question Correct
No. Answer
1 Which of the following is the use of function in python? a
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
2 Which keyword is used for function? C
a) Fun
b) Define
c) Def
d) Function
3 What will be the output of the following Python code? C
a) 3
b) 4
c) 4 is maximum
d) None of the mentioned
4 What will be the output of the following Python code? B
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
5 What will be the output of the following Python code? A
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
HelloHelloHelloHelloHello
6 What will be the output of the following Python code? C
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
7 What will be the output of the following Python code? B
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
8 Which of the following is a feature of DocString? D
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
9 What is the output of the following function call? 15
z = lambda x : x * x
print(z(6))
A. 6
B. 36
C. 0
D. error
18 Which one of the following is incorrect? B
Lab Exercises
1. Write a program to find the maximum number of three numbers by defining a function.
2. Write a program to multiply all the numbers in a list by defining a function.
3. Write a program to reverse a string by defining a function.
4. Write a function to calculate factorial of a number
5. Write a program to print the even numbers from a list which is passed as an argument to
a function.
6. Write a program to find the Greatest Common Divisor (GCD) of two numbers using
function.
7. Write a function that checks whether a passed string is palindrome or not.
8. Write a program to print the Fibonacci series up to the nth term using a function.
9. Write a function to calculate the sum of digits of a number
10. Write a function to calculate x to the power y; where x and y is passed as arguments.