Functions Book Back Questions 2
Functions Book Back Questions 2
1 If return statement is not used inside the function, the function will return:
1. 0
2. None object
3. an arbitrary integer
4. Error! Functions in Python must have a return statement.
2 Which of the following keywords marks the beginning of the function block?
1. func
2. define
3. def
4. function
3 What is the area of memory called, which stores the parameters and local variables of a
function call ?
1. a heap
2. storage area
3. a stack
4. an array
4 Find the errors in following function definitions :
def main()
print ("hello")
def func2():
print(2 + 3)
def compute():
print (x * x)
square (a)
return a * a
5 What is the default return value for a function that does not return any value explicitly ?
1. None
2. int
3. double
4. null
6 Which of the following items are present in the function header ?
1. function name only
2. both function name and parameter list
3. parameter list only
4. return value
7 What is the name given to that area of memory, where the system stores the parameters and
local variables of a function call ?
1. a heap
2. storage area
3. a stack
4. an array
8 Pick one the following statements to correctly complete the function body in the given code
snippet.
def f(number):
#Missing function body
print(f(5))
return "number"
print(number)
print("number")
return number
9 Which of the following function headers is correct ?
def f(a = 1, b):
def f(a = 1, b, c = 2):
def f(a = 1, b = 1, c = 2):
def f(a = 1, b = 1, c = 2, d):
10 Which of the following statements is not true for parameter passing to functions ?
You can pass positional arguments in any order.
You can pass keyword arguments in any order.
You can call a function with positional and keyword arguments.
Positional arguments must be before keyword arguments in a function call.
11 Which of the following is not correct in context of Positional and Default parameters in
Python functions ?
Default parameters must occur to the right of Positional parameters.
Positional parameters must occur to the right of Default parameters.
Positional parameters must occur to the left of Default parameters.
All parameters to the right of a Default parameter must also have Default values.
12 Which of the following is not correct in context of scope of variables ?
Global keyword is used to change value of a global variable in a local scope.
Local keyword is used to change value of a local variable in a global scope.
Global variables can be accessed without using the global keyword in a local scope.
Local variables cannot be used outside its scope.
13 Which of the following function calls can be used to invoke the below function definition ?
def test(a, b, c, d)
test(1, 2, 3, 4)
test(a = 1, 2, 3, 4)
test(a = 1, b = 2, c = 3, 4)
test(a = 1, b = 2, c = 3, d = 4)
14 Which of the following function calls will cause Error while invoking the below function
definition ?
def test(a, b, c, d)
test(1, 2, 3, 4)
test(a = 1, 2, 3, 4)
test(a = 1, b = 2, c = 3, 4)
test(a = 1, b = 2, c = 3, d = 4)
15 For a function header as follows :
def Calc(X,Y = 20):
Which of the following function calls will give an error ?
Calc(15, 25)
Calc(X = 15, Y = 25)
Calc(Y = 25)
Calc(X = 25)
16 What is a variable defined outside all the functions referred to as ?
A static variable
A global variable
A local variable
An automatic variable
17 What is a variable defined inside a function referred to as
A static variable
A global variable
A local variable
An automatic variable
18 Carefully observe the code and give the output or message displayed
def function1(a):
a = a + '1'
a=a*2
>>>function1("hello")
indentation Error
cannot perform mathematical operation on strings
hello2
hello2hello2
19 What is the result of this code ?
def print_double(x):
print(2 ** x)
print_double(3)
1. 8
2. 6
3. 4
4. 10
20 What is the order of resolving scope of a name in a Python program ?
BGEL
LEGB
GEBL
LBEG
21 Which of the given argument types can be skipped from a function call ?
positional arguments
keyword arguments
named arguments
default arguments
Fill in the Blanks
1 A _________________ is a subprogram that acts on data and often returns a value.
2 Python names the top level segment (main program) as _____________
3 In Python, program execution begins with first statement of _______________ segment.
4 The values being passed through a function-call statement are called _______________
5 The values received in the function definition/header are called _____________________
6 A parameter having default value in the function header is known as a _________________.
7 A ______________________ argument can be skipped in the function call statement.
8 Keyword arguments are the named arguments with assigned values being passed in the
function call statement.
9 A void function also returns a ____________ value to its caller.
10 By default, Python names the segment with top-level statements as____________
11 The _________ refers to the order in which statements are executed during a program run.
12 The default value for a parameter is defined in __________________--.
True/False Questions
1 Non-default arguments can be placed before or after a default argument in a function
definition.
2 A parameter having default value in the function header is known as a default parameter.
3 The first line of function definition that begins with keyword def and ends with a colon (:),
is also known as function header.
4 Variables that are listed within the parentheses of a function header are called function
variables.
5 In Python, the program execution begins with first statement of __main__ segment.
6 Default parameters cannot be skipped in function call.
7 The default values for parameters are considered only if no value is provided for that
parameter in the function call statement.
8 A Python function may return multiple values.
9 A void function also returns a value i.e., None to its caller.
10 Variables defined inside functions can have global scope.
11 A local variable having the same name as that of a global variable, hides the global variable
in its function.
Assertions and Reasons
(a)Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
(b)Both Assertion and Reason are true but Reason is not the correct explanation of Assertion.
Explanation
(c)Assertion is true but Reason is false.
(d) Both Assertion and Reason is false
1 Assertion. A function is a subprogram.
Reason. A function exists within a program and works within it when called.
2 Assertion. Non-default arguments cannot follow default arguments in a function call.
Reason. A function call can have different types of arguments.
3 Assertion. A parameter having a default in function header becomes optional in function call.
Reason. A function call may or may not have values for default arguments.
Answer
4 Assertion. A variable declared inside a function cannot be used outside it.
Reason. A variable created inside a function has a function scope.
5 Assertion. A variable not declared inside a function can still be used inside the function if it
is declared at a higher scope level.
Reason. Python resolves a name using LEGB rule where it checks in Local (L), Enclosing (E),
Global (G) and Built-in scopes (B), in the given order.
6 Assertion. A parameter having a default value in the function header is known as a default
parameter.
Reason. The default values for parameters are considered only if no value is provided for that
parameter in the function call statement.
7 Assertion. A function declaring a variable having the same name as a global variable, cannot
use that global variable.
Reason. A local variable having the same name as a global variable hides the global variable in
its function.
8 Assertion. If the arguments in a function call statement match the number and order of
arguments as defined in the function definition, such arguments are called positional
arguments.
Reason. During a function call, the argument list first contains default argument(s) followed
positional argument(s).
Type A: Short Answer Questions/Conceptual Questions
1 A program having multiple functions is considered better designed than a program without
any functions. Why ?
2 What all information does a function header give you about the function ?
3 What do you understand by flow of execution ?
4 What are arguments ? What are parameters ? How are these two terms different yet related ?
Give example.
5 What is the utility of : (i) default arguments, (ii) keyword arguments ?
6 Explain with a code example the usage of default arguments and keyword arguments.
7 Describe the different styles of functions in Python using appropriate examples.
8 Differentiate between fruitful functions and non-fruitful functions.
9 Can a function return multiple values ? How ?
10 What is scope ? What is the scope resolving rule of Python ?
11 What is the difference between local and global variable ?
12 When is global statement used ? Why is its use not recommended ?
13 Write the term suitable for following descriptions :
(a) A name inside the parentheses of a function header that can receive a value.
(b) An argument passed to a specific parameter using the parameter name.
(c) A value passed to a function parameter.
(d) A value assigned to a parameter name in the function header.
(e) A value assigned to a parameter name in the function call.
(f) A name defined outside all function definitions.
(g) A variable created inside a function body.
14 What do you understand by local and global scope of variables ? How can you access a
global variable inside the function, if function has a variable with same name.
Type B: Application Based Questions
Question 1(a)
What are the errors in following codes ? Correct the code and predict output :
total = 0;
def sum(arg1, arg2):
total = arg1 + arg2;
print("Total :", total)
return total;
sum(10, 20);
print("Total :", total)
1(b)
What are the errors in following codes ? Correct the code and predict output :
def Tot(Number) #Method to find Total
Sum = 0
for C in Range (1, Number + 1):
Sum += C
RETURN Sum
print (Tot[3]) #Function Calls
print (Tot[6])
Question 2
Find and write the output of the following python code :
def Call(P = 40, Q = 20):
P=P+Q
Q=P-Q
print(P, '@', Q)
return P
R = 200
S = 100
R = Call(R, S)
print(R, '@', S)
S = Call(S)
print(R, '@', S)
Question 3
Consider the following code and write the flow of execution for this. Line numbers have been
given for your reference.
1. def power(b, p):
2. y = b ** p
3. return y
4.
5. def calcSquare(x):
6. a = power(x, 2)
7. return a
8.
9. n = 5
10. result = calcSquare(n)
11. print(result)
Question 4
What will the following function return ?
def addEm(x, y, z):
print(x + y + z)
Question 5
What will the following function print when called ?
def addEm(x, y, z):
return x + y + z
print(x + y + z)
Question 6(i)
What will be the output of following program ?
num = 1
def myfunc():
return num
print(num)
print(myfunc())
print(num)
Question 6(ii)
What will be the output of following program ?
num = 1
def myfunc():
num = 10
return num
print(num)
print(myfunc())
print(num)
Question 6(iii)
What will be the output of following program ?
num = 1
def myfunc():
global num
num = 10
return num
print(num)
print(myfunc())
print(num)
Question 6(iv)
What will be the output of following program ?
def display():
print("Hello", end='')
display()
print("there!")
Question 7
Predict the output of the following code :
a = 10
y=5
def myfunc():
y=a
a=2
print("y =", y, "a =", a)
print("a + y =", a + y)
return a + y
print("y =", y, "a =", a)
print(myfunc())
print("y =", y, "a =", a)
Question 8
What is wrong with the following function definition ?
def addEm(x, y, z):
return x + y + z
print("the answer is", x + y + z)
Question 9
Write a function namely fun that takes no parameters and always returns None.
Question 10
Consider the code below and answer the questions that follow :
def multiply(number1, number2):
answer = number1 * number2
print(number1, 'times', number2, '=', answer)
return(answer)
output = multiply(5, 5)
(i) When the code above is executed, what prints out ?
(ii) What is variable output equal to after the code is executed ?
Question 11
Consider the code below and answer the questions that follow :
def multiply(number1, number2):
answer = number1 * number2
return(answer)
print(number1, 'times', number2, '=', answer)
output = multiply(5, 5)
(i) When the code above is executed, what gets printed ?
(ii) What is variable output equal to after the code is executed ?
Question 12(a)
Find the errors in code given below :
def minus(total, decrement)
output = total - decrement
print(output)
return (output)
Question 12(b)
Find the errors in code given below :
define check()
N = input ('Enter N: ')
i=3
answer = 1 + i ** 4 / N
Return answer
Question 12(c)
Find the errors in code given below :
def alpha (n, string = 'xyz', k = 10) :
return beta(string)
return n
print(alpha("Valentine's Day"):)
print(beta(string = 'true'))
print(alpha(n = 5, "Good-bye"):)
Question 13
Draw the entire environment, including all user-defined variables at the time line 10 is being
executed.
1. def sum(a, b, c, d):
2. result = 0
3. result = result + a + b + c + d
4. return result
5.
6. def length():
7. return 4
8.
9. def mean(a, b, c, d):
10. return float(sum(a, b, c, d))/length()
11.
12. print(sum(a, b, c, d), length(), mean(a, b, c, d))
Question 14
Draw flow of execution for the above program.
Question 15
Find and write the output of the following python code :
a = 10
def call():
global a
a = 15
b = 20
print(a)
call()
Question 16
In the following code, which variables are in the same scope ?
def func1():
a=1
b=2
def func2():
c=3
d=4
e=5
Question 17
Write a program with a function that takes an integer and prints the number that follows after
it. Call the function with these arguments :
4, 6, 8, 2 + 1, 4 - 3 * 2, -3 -2
Question 18
Write a program with non-void version of above function and then write flow of execution for
both the programs.
Question 19(i)
What is the output of following code fragments ?
def increment(n):
n.append([4])
return n
L = [1, 2, 3]
M = increment(L)
print(L, M)
Answer
Output
[1, 2, 3, [4]] [1, 2, 3, [4]]
Explanation
In the code, the function increment appends [4] to list n, modifying it in place. When L = [1, 2,
3], calling increment(L) changes L to [1, 2, 3, [4]]. Variable M receives the same modified list
[1, 2, 3, [4]], representing L. Thus, printing L and M results in [1, 2, 3, [4]], confirming they
reference the same list. Therefore, modifications made to list inside a function affect the
original list passed to the function.
Question 19(ii)
What is the output of following code fragments ?
def increment(n):
n.append([49])
return n[0], n[1], n[2], n[3]
L = [23, 35, 47]
m1, m2, m3, m4 = increment(L)
print(L)
print(m1, m2, m3, m4)
print(L[3] == m4)
Question 20
What will be the output of the following Python code ?
V = 25
def Fun(Ch):
V = 50
print(V, end = Ch)
V *= 2
print(V, end = Ch)
print(V, end = "*")
Fun("!")
print(V)
1. 25*50!100!25
2. 50*100!100!100
3. 25*50!100!100
4. Error
Type C: Programming Practice/Knowledge based Questions
Question 1
Write a function that takes amount-in-dollars and dollar-to-rupee conversion price; it then
returns the amount converted to rupees. Create the function in both void and non-void forms.
Question 2
Write a function to calculate volume of a box with appropriate default values for its
parameters. Your function should have the following input parameters :
(a) length of box ;
(b) width of box ;
(c) height of box.
Test it by writing complete program to invoke it.
Question 3
Write a program to have following functions :
(i) a function that takes a number as argument and calculates cube for it. The function does not
return a value. If there is no value passed to the function in function call, the function should
calculate cube of 2.
(ii) a function that takes two char arguments and returns True if both the arguments are equal
otherwise False.
Test both these functions by giving appropriate function call statements.
Question 4
Write a function that receives two numbers and generates a random number from that range.
Using this function, the main program should be able to print three numbers randomly.
Question 5
Write a function that receives two string arguments and checks whether they are same-length
strings (returns True in this case otherwise False).
Question 6
Write a function namely nthRoot( ) that receives two parameters x and n and returns nth root
of x i.e., x^(1/n).
The default value of n is 2.
Question 7
Write a function that takes a number n and then returns a randomly generated number having
exactly n digits (not starting with zero) e.g., if n is 2 then function can randomly return a
number 10-99 but 07, 02 etc. are not valid two digit numbers.
Question 8
Write a function that takes two numbers and returns the number that has minimum one's digit.
[For example, if numbers passed are 491 and 278, then the function will return 491 because it
has got minimum one's digit out of two given numbers (491's 1 is < 278's 8)].
Question 9
Write a program that generates a series using a function which takes first and last values of the
series and then generates four terms that are equidistant e.g., if two numbers passed are 1 and 7
then function returns 1 3 5 7.
Solution
def generate_series(first, last):
step = (last - first) // 3
series = [first, first + step, first + 2 * step, last]
return series