Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Chapter 3 Working with functions

TYPE B – APPLICATION BASED QUESTIONS

1. What are the errors in following codes? Correct the code and predict output:

(a) Answer
In line 3 & 4 there should be double indentation. Return should be present inside the
subprogram.

(b) Answer - In line 5 "RETURN" should be in small words and also there is only one
indentation

2. Find and write the output of the following python code :


1
OUTPUT

3. Consider the following code and write the flow of execution for this. Line numbers
have been given for your reference.

4. What will the following function return?

def addEm(x, y, z):


print (x + y + z)

Answer - When arguments are passed in addEm(x, y, z) then it add all the arguments and
print the result. It is void Function.

5. What will the following function print when called?

def addEm(x, y, z):


return x + y + z
print (x+ y + z)

Answer-When function called then it will add all the argument and it will show nothing.
Because return keyword will work before print statement.
6. What will be the output of following programs?
2
CODE 1 OUTPUT CODE 2 OUTPUT

CODE 3 OUTPUT CODE 4

OUTPUT -

7. Predict the output of the following code:


Answer - Error because at first y and a are global
variable so we cannot use y,a (variable) directly in
function.

8. What is wrong with the following function definition?


3
def addEm(x, y, z):
return x + y + z
print("the answer is", x + y + z)

Answer - Return is written before print , so when function will call then print statement will
not give any result. Function call is not present in the program.

9. Write a function namely fun that takes no parameters and always returns None.
Answer
def fun() :
print("fun")
return

Output:-
None

10. Consider the code below and answer the questions that follow:

(i) When the code above is executed, what prints out?

Answer
Output:-
5 times 5 = 25

(ii) What is variable output equal to after the code is executed?

Answer - Variable is ‘answer’. And its value is 25.

4
11. Consider the code below and answer the questions that follow:

(i) When the code above is executed, what gets printed?

Answer - It will show no result. Because return statement is written before print statement.

(ii) What is variable output equal to after the code is executed?

Answer - Variable is ‘answer’. And its value is 25

12.Find the errors in code given below: Answer

(a)
def minus (total, decrement)
output = total - decrement
print(output)
return (output)

(b) Answer
define check()
N = input ('Enter N: ')
i=3
answer = 1 + i ** 4 / N
Return answer

(c) Answer

5
def alpha (n, string ='xyz', k = 10) :
return beta(string)
return n

def beta (string)


return string == str(n)

print(alpha("Valentine's Day") :)
print(beta (string = 'true' ))
print(alpha(n = 5, "Good-bye") :)

13.Draw the entire environment, including all user-defined variables at the time line 10 is
being executed.

Answer
When line 10 executes then it must be call from line 12. At first line 12 is created in global
environment. Then it call sum and create local environment within global environment. Then sum
function return the values in global environment.

Then line 12 call length and create local environment within global environment. Then length
function return the values in global environment. Then line 12 call mean and create local
environment within global environment. Then mean function call sum and create nested local
environment. Then sum function return values in local environment. Then mean function call
length and create nested local environment. Then length function return values in local
environment.

Then mean function return values in line 12 in global environment.

6
14.Draw flow of execution for above program.

Answer

1 - 6 - 9 -12 - 1 - 2 - 3 - 4 - 12 - 6 - 7 - 12 - 9 - 10 - 1 - 2 - 3 - 4 - 10 - 6 - 7 - 10 - 12

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

Answer
Here a, b, c, d are in same scope i.e., local scope.

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

OUTPUT

7
17.Write a program with non-void version of above function and then write flow of
execution for both the programs.

Answer

Flow of execution:

1-3-1-2-3-4-1-2-4-5-1-2-5-6-1-2-6-7-1-2-7

18.What is the output of following code fragments?

(i)

Output:- [1, 2, 3, [4]] [1, 2, 3, [4]] – because mutable input


(ii)

Output:-

You might also like