UNIT II
2. FUNCTIONS AND MODULAR
PROGRAMMING
SECTION A (1 MARK – MCQ)
1. What will be the output of the following code?
def func(a, b=[]):
[Link](a)
return b
print(func(1))
print(func(2))
a) [1] and [2]
b) [1] and [1, 2]
c) [1, 2] and [1, 2]
d) Error
2. Which of the following is TRUE about Python functions?
a) Functions must always return a value
b) Functions can return multiple values as a tuple
c) Functions cannot have default arguments
d) Functions cannot be nested
3. What will be the output of the following code?
def outer():
x = 10
def inner():
return x
return inner
f = outer()
print(f())
a) 10
b) Error
c) None
d) inner
4. What is the output of the following code?
f = lambda x: x**2 if x > 0 else -x
print(f(-3))
a) 9
b) -9
c) 3
d) -3
5. Which of the following correctly defines a function with variable-length
arguments?
a)
def func(*args):
pass
b)
def func(args*):
pass
c)
def func(**args):
pass
d)
def func(args**):
pass
6. What will be the output of the following code?
def func(a, b, c=5):
return a + b + c
print(func(1, 2))
a) 3
b) 8
c) Error
d) 5
7. Which statement about lambda functions is TRUE?
a) Lambda functions can contain multiple statements
b) Lambda functions always have a return statement
c) Lambda functions can have multiple expressions
d) Lambda functions cannot take arguments
8. What will be the output of the following code?
def func(x):
return x * x
result = map(func, [1, 2, 3])
print(list(result))
a) [1, 2, 3]
b) [1, 4, 9]
c) (1, 4, 9)
d) Error
9. Which of the following demonstrates function overloading in Python?
a) Defining multiple functions with the same name but different parameters
b) Using default arguments to handle different cases
c) Using lambda functions
d) Using recursion
10. What will be the output of the following code?
def func(x):
if x == 0:
return 0
return x + func(x - 1)
print(func(3))
a) 3
b) 6
c) 9
d) Error
Answer Key
1. b
2. b
3. a
4. c
5. a
6. b
7. b
8. b
9. b
10.b
SECTION B
1. Write short notes on Python Functions.
A function is a block of code which only runs when it is called. A function can
return data as a result. A function helps avoiding code repetition.
In Python, a function is defined using the ‘def’ keyword, followed by a function
name and parentheses:
2. Explain Python Functions with arguments.
Information can be passed into functions as arguments.
Arguments are specified after the function name, inside the parentheses. You
can add as many arguments as you want, just separate them with a comma.
The following example has a function with one argument (fname). When the
function is called, we pass along a first name, which is used inside the function
to print the full name:
3. Explain Python Lambda Functions.
A lambda function is a small anonymous function.
A lambda function can take any number of arguments, but can only have one
expression.
Syntax
lambda arguments : expression
The expression is executed and the result is returned:
4. Explain Recursion Function.
Recursion is when a function calls itself.
Recursion is a common mathematical and programming concept. It means that a
function calls itself. This has the benefit of meaning that you can loop through
data to reach a result.
The developer should be very careful with recursion as it can be quite easy to
slip into writing a function which never terminates, or one that uses excess
amounts of memory or processor power. However, when written correctly
recursion can be a very efficient and mathematically-elegant approach to
programming.