0% found this document useful (0 votes)
12 views5 pages

05 Functions

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
12 views5 pages

05 Functions

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 5

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

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
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

def outer_fun(a, b):


def inner_fun(c, d):
return c + d
return inner_fun(a, b)
return a

result = outer_fun(5, 10)


print(result)
10 Which of the following items are present in the function header? D
A. function name
B. parameter list
C. return value
D. Both A and B
11 If return statement is not used inside the function, the function will return: A
A. None
B. 0
C. Null
D. Arbitary value
12 What is a recursive function? B
A. A function that calls other function.
B. A function which calls itself.
C. Both A and B
D. None of the above
13 Which of the following function headers is correct? C
A. def fun(a = 2, b = 3, c)
B. def fun(a = 2, b, c = 3)
C. def fun(a, b = 2, c = 3)
D. def fun(a, b, c = 3, d)
14 You can also create your own functions, these functions are called? B
A. built-in functions
B. user-defined functions
C. py function
D. None of the above
15 Lambda is a function in python? A
A. True
B. False
C. Lambda is a function in python but user can not use it.
D. None of the above
16 What is a variable defined outside a function referred to as? B
A. local variable
B. global variable
C. static Variable
D. automatic variable
17 What is the output of the following program? B

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

A. The variables used inside function are called local variables.


B. The local variables of a particular function can be used inside other
functions, but these cannot be used in global space
C. The variables used outside function are called global variables
D. In order to change the value of global variable inside function, keyword
global is used.

Short Answer Type

1. What is the advantage of using a function?


2. Write the syntax for defining a function.
3. What do you mean by function call?
4. What are parameters?
5. What are the function arguments?
6. What is the advantage of using default arguments?
7. Why are variable length arguments used?
8. What is the role of the return statement in function definition?
9. what do you mean by scope of a variable?
10. What is the difference between local and global variable?
11. What do you mean by anonymous function?
12. What is the syntax of the lambda function?

Descriptive Answer Type

1. Explain the flow of execution of a function calling another function.


2. The return statement is optional. Justify this statement with the help of an example.
3. Differentiate between local and global variables.
4. Write short notes with examples on i) Positional arguments ii) Keyword arguments iii)
Default arguments iv) Variable-length arguments.

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.

You might also like