0% found this document useful (0 votes)
56 views8 pages

Working With Functions - Worksheet With Answer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views8 pages

Working With Functions - Worksheet With Answer

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

THE PSBB MILLENNIUM SCHOOL

GERUGAMBAKKAM
CHENNAI
Worksheet – Working with Functions - Answer Key
1) What will be the output of the following Python code?
x = 50
def func(x):
print('x is', x)
x=2
print('Changed local x to', x)
func(x)
print('x is now', x)
Answer:
x is 50
Changed local x to 2
x is now 50
2. What will be the output of the following Python code?
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)
Answer:
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
3. What will be the output of the following Python code?
def display(b, n):
while n > 0:
print(b,end="")
n=n-1
display('z',3)
Answer:
zzz
4. What will be the output of the following
v = 80
def display(n):
global v
v = 15
if n%4==0:
v += n
else:
v -= n
print(v, end="#")
display(20)
print(v)
Answer:
35#35
5. Observe the following Python code very carefully and rewrite it after removing
all syntactical errors with each correction underlined.
DEF result_even( ):
x = input(“Enter a number”)
if (x % 2 = 0) :
print (“You entered an even number”)
else:
print(“Number is odd”)
Even ( )

6. Find and write the output of the following Python code.


def makenew(mystr):
newstr = " "

count = 0
for i in mystr:
if count%2 !=0:
newstr = newstr+str(count)
else:
if i.islower():
newstr = newstr+i.upper()
else:
newstr = newstr+i
count +=1
newstr = newstr+mystr[:1]
print("The new string is :", newstr)
makenew("sTUdeNT")
Answer:
The new string is : S111111s

7. What will be the output of the following code?


def fun(x=10, y=20):
x+=5
y=y-3
return x*y
print(fun(5),fun())
Answer:
170 255
8. What is the output of the following code?
def test():
a=96
print(a)
a =+ 2
test()
Answer:
96
9. Find the output of the following code.
def Func(hello, num):
while(num > 0):
print(hello)
num -=1
Func('A',4)
Answer:
A
A
A
A
10. What is the output of the following code?
def func(a,b=5,c=10):
print(“a:”,a,” b:”,b, “ c:”,c)
func(3,7)
func(25,c=24)
func(c=50, a=100)
Answer:
a: 3 b: 7 c: 10
a: 25 b: 5 c: 24
a: 100 b: 5 c: 50
11. Give the output
def abc(x,y=60):
print(x+y)
a=20
b=30
abc(a,b)
abc(a)
abc(b)
Answer:
50
80
90
12. Write a Flow of execution of the following code? Also find the
output
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)
Answer
Flow of execution
1-6-7-8-1-2-3-4-5-8-9-10-1-2-3-4-5-10-11
Output
300 @ 200
300 @ 100
120 @ 100
300 @ 120

13. Find Error of the following code.


def calc(u):
if u%2==0
return u+10;
else:
return u+2
def pattern(M,B=2):
for CNT in range(0,B):
print(calc(CNT), M, end=””);
pattern(*)
pattern(“#”,4)
pattern(@.3)
14. Write a statement to call the function.
def Add():
X = 10 + 20
print(X)
Add() #statement to call the above function
15. Write a statement to call the function.
def Add(X,Y):
Z = X+Y
print(Z)
Add(10,20) #statement to call the above function
16. Write a statement to call the function.
def Add(X,Y):
Z = X+Y
return Z
C=Add(10,20) # statement to call the above function
print(“Total =”,C)
17. Which Line Number Code will never execute?
def Check(num): #Line 1
if num%2==0: #Line 2
print("Hello") #Line 3
return True #Line 4
print("Bye") #Line 5
else: #Line 6
return False #Line 7
C = Check(20) #Line 8
print(C) #Line 9
18. Call the given function using KEYWORD ARGUMENT with values 100 and 200.
Def Swap(num1,num2):
num1,num2=num2,num1
print(num1,num2)
Swap(num1=100,num2=200)
19. What will be the output of the following code?
def drawline(char='$',time=5):
print(char*time)
drawline()
drawline('@',10)
drawline(65)
drawline(chr(65))
Answer:
$$$$$
@@@@@@@@@@
325
AAAAA
20. Which of the following function headers is correct?
(a) def myFunc(x = 2, y = 3, z):
(b) def myFunc(x = 2, y, z = 3):
(c) def myFunc(x, y = 2, z = 3):
(d) def myFunc(x, y, z = 3, p):
21. If the return statement is not used in the function then which type of value will
be returned by the function?
a) int b) str c) float d) None
22. Divya wants to print the identity of the object used in the function. Which of
the following functions is used to print the same?
a) identity() b) ide() c) id() d) idy()
23. The default value for a parameter is defined in function ?
a. Definition b. Return statement
c. Header d. None of the above
24. Function randint() is available under which module of python?
a. random b. math c. statistics d. None
25. By default if you return multiple value separated by comma, then it is returned
as tuple__
26. In keyword arguments we can skip the default argument, but all the
keyword arguments should match the parameters in the function definitions.
27. def keyword is used to define a function.
28. Function will perform its action only when it is called.
29. A python function may return multiple values?
a. True b. False
30. A python program execution begins with first statement of _main_ segment?
a. True b. False
Please choose the correct choice out of the four options given below:
a. Both Assertion and reason are true and reason is the correct explanation of
assertion.
b. Assertion and reason both are true but reason is not the correct explanation
of assertion.
c. Assertion is true, reason is false.
d. Assertion is false, reason is true
1. Assertion: The function header ‘def read (a=2, b=5, c):’ is not correct.
Reason: Non default arguments can’t follow default arguments.
a. Both Assertion and reason are true and reason is the correct explanation
of assertion.
2. Assertion: Every function returns a value if the function does not explicitly return a
value, then it will return ‘Zero’.
Reason: Zero is not equivalent to None.
d. Assertion is false, reason is true
3. Consider the following code:
x = 100
def study( ):
global x
x = 50
print(x)
Assertion: 50 will be the output of above code.
Reason: Because the x used inside the function study( ) is of local scope.
a. Both Assertion and reason are true and reason is the correct explanation
of assertion.
4. Assertion (A): Keyword arguments are related to the function calls
Reason (R): When you use keyword arguments in a function call, the caller
identify
the arguments by the parameter name.
a. Both Assertion and reason are true and reason is the correct explanation of
assertion.

5. Assertion (A): global keyword is used inside a function to enable the function
alter the value of a global variable.
Reasoning (R): A global variable cannot be accessed by a function without the
use of global keyword.
a. Both Assertion and reason are true and reason is the correct
explanation of assertion

6. Study the following program and select the possible output(s) and write maximum
and minimum value assigned to the variable y .
import random
x=random.random( )
y=random.randint(0,4)
print(int(x),”:”,y+int(x))
(i) 0:0 (ii) 1: 6 (iii) 2:4 (iv) 0:3
7. What possible output(s) are expected to be displayed on screen at the time of
execution of the following code? Also specify the maximum and minimum value
that can be assigned to variable X.
import random
L=[10,7,21]
X=random.randint(1,2)
for i in range(X):
Y=random.randint(1,X)
print(L[Y],”$”,end=” ”)
(a) 10 $ 7 $ (b) 21 $ 7 $ (c) 21 $ 10 $ (d) 7 $ 37
Maximum value that can be assigned to variable X is 2
Minimum value that can be assigned to variable X is 1

31.

You might also like