Python Questions – 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
2. Which keyword is used for function?
a) Fun
b) Define
c) def
d) 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
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
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
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
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
HelloHelloHelloHelloHello
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
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
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
11. Which are the advantages of functions in python?
a) Reducing duplication of code
b) Decomposing complex problems into simpler pieces
c) Improving clarity of the code
d) All of the mentioned
12. What are the two main types of functions?
a) Custom function
b) Built-in function & User defined function
c) User function
d) System function
13. Where is function defined?
a) Module
b) Class
c) Another function
d) All of the mentioned
14. What is called when a function is defined inside a class?
a) Module
b) Class
c) Another function
d) Method
15. Which of the following is the use of id() function in python?
a) Id returns the identity of the object
b) Every object doesn’t have a unique id
c) All of the mentioned
d) None of the mentioned
16. Which of the following refers to mathematical function?
a) sqrt
b) rhombus
c) add
d) rhombus
17. What will be the output of the following Python code?
def cube(x):
return x * x * x
x = cube(3)
print (x)
a) 9
b) 3
c) 27
d) 30
18. What will be the output of the following Python code?
def C2F(c):
return c * 9/5 + 32
print(C2F(100))
print(C2F(0))
a)
212.0
32.0
b)
314.0
24.0
c)
567.0
98.0
d) None of the mentioned
19. What will be the output of the following Python code?
def power(x, y=2):
r=1
for i in range(y):
r=r*x
return r
print (power(3))
print (power(3, 3))
a)
212
32
b)
9
27
c)
567
98
d) None of the mentioned
20. What will be the output of the following Python code?
def sum(*args):
'''Function returns the sum of all values'''
r=0
for i in args:
r += i
return r
print(sum.__doc__)
print(sum(1, 2, 3))
print(sum(1, 2, 3, 4, 5))
a)
Function returns the sum of all values
6
15
b)
6
100
c)
123
12345
d) None of the mentioned
21.Python supports the creation of anonymous functions at runtime, using a construct called
__________
a) lambda
b) pi
c) anonymous
d) none of the mentioned
22. What will be the output of the following Python code?
y=6
z = lambda x: x * y
print (z(8))
a) 48
b) 14
c) 64
d) None of the mentioned
23. What will be the output of the following Python code?
lamb = lambda x: x ** 3
print(lamb(5))
a) 15
b) 555
c) 125
d) None of the mentioned
24. Does Lambda contains return statements?
a) True
b) False
25. Lambda is a statement.
a) True
b) False
26. Lambda contains block of statements.
a) True
b) False
27. What will be the output of the following Python code?
def f(x, y, z): return x + y + z
print(f(2, 30, 400))
a) 432
b) 24000
c) 430
d) No output
28. What will be the output of the following Python code?
def writer():
title = 'Sir'
name = (lambda x:title + ' ' + x)
return name
who = writer()
print(who('Arthur'))
a) Arthur Sir
b) Sir Arthur
c) Arthur
d) None of the mentioned
29. What will be the output of the following Python code?
L = [lambda x: x ** 2,
lambda x: x ** 3,
lambda x: x ** 4]
for f in L:
print(f(3))
a)
27
81
343
b)
6
9
12
c)
9
27
81
d) None of the mentioned
30. What will be the output of the following Python code?
min = (lambda x, y: x if x < y else y)
print(min(101*99, 102*98))
a) 9997
b) 9999
c) 9996
d) None of the mentioned