Python Function Basics Quiz
Python Function Basics Quiz
Chapter 3
Working with Functions
Checkpoint 3.1
Question 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.
Answer
None object
Reason — The default return value for a function that does not return any value explicitly is None.
Question 2
Which of the following keywords marks the beginning of the function block?
1. func
2. define
3. def
4. function
Answer
def
def function_name(parameters):
statements
Question 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
Answer
a stack
Reason — A stack is the area of memory, which stores the parameters and local variables of a function call.
Question 4
1. def main()
2. print ("hello")
3. def func2():
4. print(2 + 3)
5. def compute():
6. print (x * x)
7. square (a)
8. return a * a
Answer
1. def main():
2. print("hello")
3. def func2():
4. print(2 + 3)
5. def compute(x):
6. print (x * x)
7. def square(a):
8. return a * a
Question 1
What is the default return value for a function that does not return any value explicitly ?
1. None
2. int
3. double
4. null
Answer
None
Reason — The default return value for a function that does not return any value explicitly is None.
Question 2
Answer
Reason — Function header is the first line of function definition that begins with keyword def and ends with a
colon (:), specifies the name of the function and its parameters.
Question 3
Which of the following keywords marks the beginning of the function block ?
1. func
2. define
3. def
4. function
Answer
def
def function_name(parameters):
statements
Question 4
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
Answer
a stack
Reason — A stack is the area of memory, which stores the parameters and local variables of a function call.
Question 5
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))
1. return "number"
2. print(number)
3. print("number")
4. return number
Answer
return number
Question 6
Answer
Reason — In a function header, any parameter cannot have a default value unless all parameters appearing on
its right have their default values.
Question 7
Which of the following statements is not true for parameter passing to functions ?
Answer
Reason — Positional arguments are passed to a function based on their positions in the function call statement.
The first positional argument in the function call is assigned to the first parameter in the function definition, the
second positional argument is assigned to the second parameter, and so on.
Question 8
Which of the following is not correct in context of Positional and Default parameters in Python functions ?
Answer
Positional parameters must occur to the right of Default parameters.
Reason — The positional parameters must occur to the left of default parameters because in a function header,
any parameter cannot have a default value unless all parameters appearing on its right have their default values.
Question 9
Answer
Reason — If you need to modify a local variable in global scope, you do so by passing it as an argument to a
function and/or returning a modified value from the function.
Question 10
Which of the following function calls can be used to invoke the below function definition ?
def test(a, b, c, d)
1. test(1, 2, 3, 4)
2. test(a = 1, 2, 3, 4)
3. test(a = 1, b = 2, c = 3, 4)
4. test(a = 1, b = 2, c = 3, d = 4)
Answer
test(1, 2, 3, 4)
test(a = 1, b = 2, c = 3, d = 4)
Reason —
1. The function call test(1, 2, 3, 4) passes four positional arguments 1, 2, 3, and 4 to the function.
2. The function call test(a = 1, b = 2, c = 3, d = 4) passes four keyword arguments,
explicitly stating the parameter names (a, b, c, d) and their corresponding values.
Question 11
Which of the following function calls will cause Error while invoking the below function definition ?
def test(a, b, c, d)
1. test(1, 2, 3, 4)
2. test(a = 1, 2, 3, 4)
3. test(a = 1, b = 2, c = 3, 4)
4. test(a = 1, b = 2, c = 3, d = 4)
Answer
test(a = 1, 2, 3, 4)
test(a = 1, b = 2, c = 3, 4)
Reason — In both of these function call test(a = 1, 2, 3, 4) and test(a = 1, b = 2, c = 3, 4),
the syntax is incorrect because when using keyword arguments, all arguments following the first one must also
be specified with keyword arguments.
Question 12
1. Calc(15, 25)
2. Calc(X = 15, Y = 25)
3. Calc(Y = 25)
4. Calc(X = 25)
Answer
Calc(Y = 25)
Reason — The function call statement must match the number and order of arguments as defined in the
function definition. Here, the X positional argument is missing.
Question 13
1. A static variable
2. A global variable
3. A local variable
4. An automatic variable
Answer
A global variable
Reason — A global variable is a variable declared in top level segment (__main__) of a program and usable
inside the whole program and all blocks contained within the program.
Question 14
1. A static variable
2. A global variable
3. A local variable
4. An automatic variable
Answer
A local variable
Reason — A local variable is a variable declared in a function-body and it can be used only within this function
and the other blocks contained under it.
Question 15
Carefully observe the code and give the answer.
def function1(a):
a = a + '1'
a = a * 2
>>>function1("hello")
1. indentation Error
2. cannot perform mathematical operation on strings
3. hello2
4. hello2hello2
Answer
indentation error
Reason — The line a = a * 2 should be indented backwards otherwise it will give an error.
Question 16
def print_double(x):
print(2 ** x)
print_double(3)
1. 8
2. 6
3. 4
4. 10
Answer
Reason — The code defines a function named print_double that takes one parameter x. Inside the function,
it calculates 2 raised to the power of x using the exponentiation operator **, and then prints the result. So, when
we call print_double(3), we're passing 3 as an argument to the function. The function calculates 2 ** 3,
which is 8, and prints the result.
Question 17
1. BGEL
2. LEGB
3. GEBL
4. LBEG
Answer
LEGB
Reason — When you access a variable from within a program or function, python follows name resolution rule,
also known as LEGB rule. When python encounters a name (variable or function), it first searches the local
scope (L), then the enclosing scope (E), then the global scope (G), and finally the built-in scope (B).
Question 18
Which of the given argument types can be skipped from a function call ?
1. positional arguments
2. keyword arguments
3. named arguments
4. default arguments
Answer
default arguments
Reason — A default argument is an argument with a default value in the function header, making it optional in
the function call. The function call may or may not have a value for it.
Question 1
Question 2
Question 3
Question 4
The values being passed through a function-call statement are called arguments / actual parameters / actual
arguments.
Question 5
The values received in the function definition/header are called parameters / formal parameters / formal
arguments.
Question 6
A parameter having default value in the function header is known as a default parameter.
Question 7
Question 8
Keyword arguments are the named arguments with assigned values being passed in the function call statement.
Question 9
By default, Python names the segment with top-level statements (main program) as __main__.
Question 11
The flow of execution refers to the order in which statements are executed during a program run.
Question 12
True/False Questions
Question 1
Non-default arguments can be placed before or after a default argument in a function definition.
Answer
False
Reason — In a function header, any parameter cannot have a default value unless all parameters appearing on
its right have their default values. Hence, non-default arguments cannot follow default argument.
Question 2
A parameter having default value in the function header is known as a default parameter.
Answer
True
Reason — A default parameter is a parameter having default value in the function header.
Question 3
The first line of function definition that begins with keyword def and ends with a colon (:), is also known as
function header.
Answer
True
Reason — The first line of function definition that begins with keyword def and ends with a colon (:), which
specifies the name of the function and its parameter is called function header.
Question 4
Variables that are listed within the parentheses of a function header are called function variables.
Answer
False
Reason — Variables that are listed within the parentheses of a function header are called parameters.
Question 5
In Python, the program execution begins with first statement of __main__ segment.
Answer
True
Reason — The Python interpreter starts the execution of a program from top-level statements. The top-level
statements are part of main program, __main__ .
Question 6
Answer
False
Reason — A default parameter in function header becomes optional in function call. Function call may or may
not have value for default parameters.
Question 7
The default values for parameters are considered only if no value is provided for that parameter in the function
call statement.
Answer
True
Reason — The default values for parameters are considered only if no value is provided for that parameter in
the function call statement.
Question 8
Answer
True
Reason — A Python function may return multiple values i.e., more than one value from a function.
Question 9
Answer
True
Reason — A void function do not return a value but they return a legal empty value of Python i.e., None to its
caller.
Question 10
Answer
False
Reason — Variables defined inside functions have only local scope. This means that they are only accessible
within the function in which they are defined.
Question 11
A local variable having the same name as that of a global variable, hides the global variable in its function.
Answer
True
Reason — Inside a function, you assign a value to a name which is already there in a global scope, Python
won't use the global scope variable because it is an assignment statement and assignment statement creates a
variable by default in current environment. That means a local variable having the same name as that of a global
variable, hides the global variable in its function.
Question 1
Reason. A function exists within a program and works within it when called.
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
A function is a subprogram that acts on data and often returns a value. It is a self-contained unit of code that
performs a specific task within a larger program. Functions are defined within a program and can be called upon
to execute a specific set of instructions when needed.
Question 2
Answer
(b)
Both Assertion and Reason are true but Reason is not the correct explanation of Assertion.
Explanation
In a function call, any parameter cannot have a default value unless all parameters appearing on its right have
their default values. Hence, non-default arguments cannot follow default arguments in a function call. Python
supports three types of formal arguments :
1. Positional arguments
2. Default arguments
3. keyword arguments
Question 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
(a)
Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
When a parameter in a function header has a default value, it means that if no value is provided for that
parameter during the function call, the default value will be used. This effectively makes the parameter optional
in the function call. When calling a function with default arguments, we have the option to either provide values
for those arguments or omit them, in which case the default values will be used.
Question 4
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
Variables declared inside a function are typically local to that function, meaning they exist only within the scope
of the function and they can only be accessed and used within the function in which they are declared.
Question 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.
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
In Python, if a variable is not declared inside a function but is declared at a higher scope level (e.g., in the global
scope or an enclosing scope), the function can still access and use that variable. Since Python follows the LEGB
rule, variables declared at higher scope levels can be accessed within functions.
Question 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.
Answer
(b)
Both Assertion and Reason are true but Reason is not the correct explanation of Assertion.
Explanation
A default parameter is a parameter in a function definition that has a predefined value. When we call a function
with default parameters, if we don't provide a value for a parameter, the default value specified in the function
header will be used instead.
Question 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.
Answer
(a)
Both Assertion and Reason are true and Reason is the correct explanation of Assertion.
Explanation
Inside a function, you assign a value to a name which is already there in global scope, Python won't use the
global scope variable because it is an assignment statement and assignment statement creates a variable by
default in current environment. That means a local variable having the same name as a global variable hides the
global variable in its function.
Question 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).
Answer
(c)
Explanation
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. During a function call, the argument list first
contains positional argument followed by default arguments because any parameter cannot have a default value
unless all parameters appearing on its right have their default values.
Question 1
A program having multiple functions is considered better designed than a program without any functions. Why ?
Answer
A program having multiple functions is considered better designed than a program without any functions
because
1. It makes program handling easier as only a small part of the program is dealt with at a time, thereby
avoiding ambiguity.
2. It reduces program size.
3. Functions make a program more readable and understandable to a programmer thereby making
program management much easier.
Question 2
What all information does a function header give you about the function ?
Answer
Function header is the first line of function definition that begins with keyword def and ends with a colon (:),
specifies the name of the function and its parameters.
Question 3
Answer
Flow of execution refers to the order in which statements are executed during a program run.
Question 4
What are arguments ? What are parameters ? How are these two terms different yet related ? Give example.
Answer
Arguments — The values being passed through a function-call statement are called arguments.
Parameters — The values received in the function definition/header are called parameters.
Arguments appear in function call statement whereas parameters appear in function header. The two terms are
related because the values passed through arguments while calling a function are received by parameters in the
function definition.
For example :
Question 5
Answer
(i) Default arguments — Default arguments are useful in case a matching argument is not passed in the
function call statement. They give flexibility to specify the default value for a parameter so that it can be
skipped in the function call, if needed. However, still we cannot change the order of the arguments in the
function call.
(ii) Keyword arguments — Keyword arguments are useful when you want to specify arguments by their
parameter names during a function call. This allows us to pass arguments in any order, as long as we specify the
parameter names when calling the function. It also makes the function call more readable and self-explanatory.
Question 6
Explain with a code example the usage of default arguments and keyword arguments.
Answer
Default arguments — Default arguments are used to provide a default value to a function parameter if no
argument is provided during the function call.
For example :
greet("Alice")
greet("Bob", "Hi there")
Output
Hello Alice
Hi there Bob
In this example, the message parameter has a default value of "Hello". If no message argument is provided
during the function call, the default value is used.
Keyword arguments — Keyword arguments allow us to specify arguments by their parameter names during a
function call, irrespective of their position.
Output
Both default arguments and keyword arguments provide flexibility and improve the readability of code,
especially in functions with multiple parameters or when calling functions with optional arguments.
Question 7
Answer
1. Built-in functions — These are pre-defined functions and are always available for use. For example:
In the above example, print(), len(), input() etc. are built-in functions.
11. Functions defined in modules — These functions are pre-defined in particular modules and can only
be used when corresponding module is imported. For example:
Question 8
Answer
Functions returning some value are called as Functions that does not return any value are called as non-fruitful
fruitful functions. functions.
They are also called as non-void functions. They are also called as void functions.
They have return statements in the syntax They may or may not have a return statement, but if they do, it typically
: return<value>. appears as per syntax : return.
Fruitful functions return some computed result in Non-fruitful functions return legal empty value of Python, which is None,
terms of a value. to their caller.
Question 9
Answer
Yes, a function can return multiple values. To return multiple values from a function, we have to ensure
following things :
2. The function call statement should receive or use the returned values in one of the following ways :
(b) Or we can directly unpack the received values of tuple by specifying the same number of variables on the
left-hand side of the assignment in function call.
Question 10
Answer
Scope refers to part(s) of program within which a name is legal and accessible. When we access a variable from
within a program or function, Python follows name resolution rule, also known as LEGB rule. When Python
encounters a name (variable or function), it first searches the local scope (L), then the enclosing scope (E), then
the global scope (G), and finally the built-in scope (B).
Question 11
Answer
A local variable is a variable defined within a function. A global variable is a variable defined in the 'main' program.
They are only accessible within the block in which they are They are accessible from anywhere within the program,
defined. including inside functions.
These variables have local scope. These variables have global scope.
The lifetime of a local variable is limited to the block of code in Global variables persist throughout the entire execution of
which it is defined. Once the execution exits that block, the local the program. They are created when the program starts and
variable is destroyed, and its memory is released. are only destroyed when the program terminates.
Question 12
Answer
If we want to assign some value to the global variable without creating any local variable then global statement
is used. It is not recommended because once a variable is declared global in a function, we cannot undo the
statement. That is, after a global statement, the function will always refer to the global variable and local
variable cannot be created of the same name. Also, by using global statement, programmers tend to lose control
over variables and their scopes.
Question 13
Answer
(a) Parameter
(c) Argument
Question 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.
Answer
Local scope — Variables defined within a specific block of code, such as a function or a loop, have local scope.
They are only accessible within the block in which they are defined.
Global scope — Variables defined outside of any specific block of code, typically at the top level of a program
or module, have global scope. They are accessible from anywhere within the program, including inside
functions.
To access a global variable inside a function, even if the function has a variable with the same name, we can use
the global keyword to declare that we want to use the global variable instead of creating a new local variable.
The syntax is :
global<variable name>
For example:
def state1():
global tigers
tigers = 15
print(tigers)
tigers = 95
print(tigers)
state1()
print(tigers)
In the above example, tigers is a global variable. To use it inside the function state1 we have
used global keyword to declare that we want to use the global variable instead of creating a new local variable.
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)
Answer
total = 0
def sum(arg1, arg2):
total = arg1 + arg2
print("Total :", total)
return total
sum(10, 20)
print("Total :", total)
Output
Total : 30
Total : 0
Explanation
Question 1(b)
What are the errors in following codes ? Correct the code and predict output :
Answer
Output
6
21
Explanation
1. There should be a colon (:) at the end of the function definition line to indicate the start of the function
block.
2. Python's built-in function for generating sequences is range(), not Range().
3. Python keywords like return should be in lowercase.
4. When calling a function in python, the arguments passed to the function should be enclosed inside
parentheses () not square brackets [].
Question 2
Answer
Output
300 @ 200
300 @ 100
120 @ 100
300 @ 120
Explanation
1. Function Call is defined with two parameters P and Q with default values 40 and 20 respectively.
2. Inside the function Call, P is reassigned to the sum of its original value P and the value of Q.
3. Q is reassigned to the difference between the new value of P and the original value of Q.
4. Prints the current values of P and Q separated by @.
5. The function returns the final value of P.
6. Two variables R and S are initialized with values 200 and 100 respectively.
7. The function Call is called with arguments R and S, which are 200 and 100 respectively. Inside the
function, P becomes 200 + 100 = 300 and Q becomes 300 - 100 = 200. So, 300 and 200 are printed. The
function returns 300, which is then assigned to R. Therefore, R becomes 300.
8. S = Call(S) — The function Call is called with only one argument S, which is 100. Since the
default value of Q is 20, P becomes 100 + 20 = 120, and Q becomes 120 - 20 = 100. So, 120 and 100 are
printed. The function returns 120, which is then assigned to S. Therefore, S becomes 120.
Question 3
Consider the following code and write the flow of execution for this. Line numbers have been given for your
reference.
Answer
1 → 5 → 9 → 10 → 5 → 6 → 1 → 2 → 3 → 6 → 7 → 10 → 11
Explanation
Line 1 is executed and determined that it is a function header, so entire function-body (i.e., lines 2 and 3) is
ignored. Line 5 is executed and determined that it is a function header, so entire function-body (i.e., lines 6 and
7) is ignored. Lines 9 and 10 are executed, line 10 has a function call, so control jumps to function header (line
5) and then to first line of function-body, i.e., line 6, it has a function call , so control jumps to function header
(line 1) and then to first line of function-body, i.e, line 2. Function returns after line 3 to line 6 and then returns
after line 7 to line containing function call statement i.e, line 10 and then to line 11.
Question 4
Answer
The function addEm will return None. The provided function addEm takes three parameters x, y, and z,
calculates their sum, and then prints the result. However, it doesn't explicitly return any value. In python, when a
function doesn't have a return statement, it implicitly returns None. Therefore, the function addEm will
return None.
Question 5
Answer
Explanation
num = 1
def myfunc():
return num
print(num)
print(myfunc())
print(num)
Answer
Output
1
1
1
Explanation
The code initializes a global variable num with 1. myfunc just returns this global variable. Hence, all the
three print statements print 1.
Question 6(ii)
num = 1
def myfunc():
num = 10
return num
print(num)
print(myfunc())
print(num)
Answer
Output
1
10
1
Explanation
1. num = 1 — This line assigns the value 1 to the global variable num.
2. def myfunc() — This line defines a function named myfunc.
3. print(num) — This line prints the value of the global variable num, which is 1.
4. print(myfunc()) — This line calls the myfunc function. Inside myfunc function, num =
10 defines a local variable num and assigns it the value of 10 which is then returned by the function. It
is important to note that the value of global variable num is still 1 as num of myfunc is local to it and
different from global variable num.
5. print(num) — This line prints the value of the global variable num, which is still 1.
Question 6(iii)
Answer
Output
1
10
10
Explanation
1. num = 1 — This line assigns the value 1 to the global variable num.
2. def myfunc() — This line defines a function named myfunc.
3. print(num) — This line prints the value of the global variable num, which is 1.
4. print(myfunc()) — This line calls the myfunc function. Inside the myfunc function, the value 10
is assigned to the global variable num. Because of the global keyword used earlier, this assignment
modifies the value of the global variable num to 10. The function then returns 10.
5. print(num) — This line prints the value of the global variable num again, which is still 1.
Question 6(iv)
def display():
print("Hello", end='')
display()
print("there!")
Answer
Output
Hellothere!
Explanation
The function display prints "Hello" without a newline due to the end='' parameter. When called, it prints
"Hello". Outside the function, "there!" is printed on the same line due to the absence of a newline.
Question 7
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)
Answer
Explanation
In the provided code, the global variables a and y are initialized to 10 and 5, respectively. Inside
the myfunc function, the line a = 2 suggests that a is a local variable of myfunc. But the line before it, y =
a is trying to assign the value of local variable a to local variable y even before local variable a is defined.
Therefore, this code raises an UnboundLocalError.
Question 8
Answer
In the above function definition, the line print("the answer is", x + y + z) is placed after the return
statement. In python, once a return statement is encountered, the function exits immediately, and any subsequent
code in the function is not executed. Therefore, the print statement will never be executed.
Question 9
Write a function namely fun that takes no parameters and always returns None.
Answer
def fun():
return
Explanation
def fun():
return
r = fun()
print(r)
The function fun() returns None. When called, its return value is assigned to r, which holds None.
Then print(r) outputs None.
Question 10
Consider the code below and answer the questions that follow :
Answer
5 times 5 = 25
(ii) After the code is executed, the variable output is equal to 25. This is because the function multiply returns
the result of multiplying 5 and 5, which is then assigned to the variable output.
Question 11
Consider the code below and answer the questions that follow :
Answer
(i) When the code above is executed, it will not print anything because the print statement after the return
statement won't execute. Therefore, the function exits immediately after encountering the return statement.
(ii) After the code is executed, the variable output is equal to 25. This is because the function multiply returns
the result of multiplying 5 and 5, which is then assigned to the variable output.
Question 12(a)
Answer
define check()
N = input ('Enter N: ')
i = 3
answer = 1 + i ** 4 / N
Return answer
Answer
def check():
N = int(input('Enter N:'))
i = 3
answer = 1 + i ** 4 / N
return answer
Question 12(c)
print(alpha("Valentine's Day"):)
print(beta(string = 'true'))
print(alpha(n = 5, "Good-bye"):)
Answer
1. The second return statement in the alpha function (return n) is unreachable because the first return
statement return beta(string) exits the function.
2. The function definition lacks colon at the end. The variable n in the beta function is not defined. It's
an argument to alpha, but it's not passed to beta explicitly. To access n within beta, we need to
either pass it as an argument or define it as a global variable.
3. There should not be colon at the end in the function call.
4. In the function call beta(string = 'true'), there should be argument for parameter n.
5. In the function call alpha(n = 5, "Good-bye"), the argument "Good-bye" lacks a keyword. It
should be string = "Good-bye".
print(alpha("Valentine's Day"))
print(beta(string='true', n=10))
print(alpha(n=5, string="Good-bye"))
Question 13
Draw the entire environment, including all user-defined variables at the time line 10 is being executed.
Answer
Answer
1 → 6 → 9 → 12 → 1 → 2 → 3 → 4 → 6 → 7 → 9 → 10 → 12
Line 1 is executed and determined that it is a function header, so entire function-body (i.e, lines 2, 3, 4) is
[Link] line 6 is executed and determined that it is a function header, so entire function-body (i.e, line 7) is
ignored, Line 9 is executed and determined that it is a function header, so entire function-body (i.e, line 10) is
ignored. Then line 12 is executed and it has a function calls, so control jumps to the function header (line 1) and
then first line of function-body, i.e, line 2, function returns after line 4 to function call line (line 12) and then
control jumps to line 6, it is a function header and then first line of function-body, i.e., line 7, function returns
after line 7 to function call line (line 12) and then control jumps to line 9, it is a function header and then first
line of function-body, i.e., line 10, function returns after line 10 to function call line 12.
Question 15
a = 10
def call():
global a
a = 15
b = 20
print(a)
call()
Answer
Output
15
Explanation
Question 16
def func1():
a = 1
b = 2
def func2():
c = 3
d = 4
e = 5
Answer
In the code, variables a and b are in the same scope because they are defined within the same
function func1(). Similarly, variables c and d are in the same scope because they are defined within the same
function func2(). e being a global variable is not in the same scope.
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
Answer
1. def print_number(number):
2. next_number = number + 1
3. print("The number following", number, "is", next_number)
4. print_number(4)
5. print_number(6)
6. print_number(8)
7. print_number(2 + 1)
8. print_number(4 - 3 * 2)
9. print_number(- 3 - 2)
Output
Explanation
1. def print_number(number) — This line defines a function named print_number that takes one
argument number.
2. next_number = number + 1 — Inside the print_number function, this line calculates the next
number after the input number by adding 1 to it and assigns the result to the variable next_number.
3. print("The number following", number, "is", next_number) — Inside
the print_number function, this line prints a message stating the number and its following number.
4. Then the print_number function is called multiple times with 4, 6, 8, 3 ,((4 - 3 * 2) = -2), ((-3-2) = -
5) as arguments.
Question 18
Write a program with non-void version of above function and then write flow of execution for both the
programs.
Answer
1. def print_number(number):
2. next_number = number + 1
3. return next_number
4. print(print_number(4))
5. print(print_number(6))
6. print(print_number(8))
7. print(print_number(2 + 1))
8. print(print_number(4 - 3 * 2))
9. print(print_number(-3 - 2))
Output
5
7
9
4
-1
-4
Explanation
[Link] print_number(number) — This line defines a function named print_number that takes one
argument number.
2. next_number = number + 1 — Inside the print_number function, this line calculates the next
number after the input number by adding 1 to it and assigns the result to the variable next_number.
3. return next_number — Inside the print_number function, this line returns next_number.
4. Then the print_number function is called multiple times with 4, 6, 8, 3 ,((4 - 3 * 2) = -2), ((-3-2) = -
5) as arguments.
The flow of execution for the above program with non-void version is as follows :
1→4→1→2→3→4→5→1→2→3→5→6→1→2→3→6→7→1→2→3→7→8→1
→2→3→8→9→1→2→3→9
Line 1 is executed and determined that it is a function header, so entire function-body (i.e., line 2 and 3) is
ignored. Then line 4 is executed and it has function call, so control jumps to the function header (line 1) and
then to first line of function-body, i.e., line 2, function returns after line 3 to line containing function call
statement i.e., line 4. The next lines 5, 6, 7, 8, 9 have function calls so they repeat the above steps.
1→4→1→2→3→4→5→1→2→3→5→6→1→2→3→6→7→1→2→3→7→8→1
→2→3→8→9→1→2→3→9
Line 1 is executed and determined that it is a function header, so entire function-body (i.e., line 2 and 3) is
ignored. Then line 4 is executed and it has function call, so control jumps to the function header (line 1) and
then to first line of function-body, i.e., line 2, function returns after line 3 to line containing function call
statement i.e., line 4. The next lines 5, 6, 7, 8, 9 have function calls so they repeat the above steps.
Question 19(i)
def increment(n):
[Link]([4])
return n
L = [1, 2, 3]
M = increment(L)
print(L, M)
Answer
Output
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)
def increment(n):
[Link]([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)
Answer
Output
The function increment appends [49] to list n and returns its first four elements individually. When L =
[23, 35, 47], calling increment(L) modifies L to [23, 35, 47, [49]]. Variables m1, m2, m3, and
m4 are assigned the same list [23, 35, 47, [49]], representing the original list L. Thus, printing L and m1, m2,
m3, m4 yields [23, 35, 47, [49]]. The expression L[3] == m4 evaluates to True, indicating that the fourth
element of L is the same as m4.
Question 20
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
Answer
25*50!100!25
Explanation
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.
Solution
Output
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 :
Solution
default_volume = calculate_volume()
print("Volume of the box with default values:", default_volume)
v = calculate_volume(10, 7, 15)
print("Volume of the box with default values:", v)
b = calculate_volume(width = 19)
print("Volume of the box with default values:", b)
Output
Question 3
(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.
Solution
calculate_cube(3)
calculate_cube()
char1 = 'a'
char2 = 'b'
print("Characters are equal:", check_equal_chars(char1, char1))
print("Characters are equal:", check_equal_chars(char1, char2))
Output
Cube of 3 is 27
Cube of 2 is 8
Characters are equal: True
Characters are equal: False
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.
Solution
import random
for i in range(3):
random_num = generate_random_number(num1, num2)
print("Random number between", num1, "and", num2, ":", random_num)
Output
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).
Solution
s1 = "hello"
s2 = "world"
s3 = "python"
print(same_length_strings(s1, s2))
print(same_length_strings(s1, s3))
Output
True
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.
Solution
result = nthRoot(x, n)
print("The", n, "th root of", x, "is:", result)
default_result = nthRoot(x)
print("The square root of", x, "is:", default_result)
Output
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.
Solution
import random
def generate_number(n):
lower_bound = 10 ** (n - 1)
upper_bound = (10 ** n) - 1
return [Link](lower_bound, upper_bound)
Output
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)].
Solution
Output
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
Output