Working With Functions - Worksheet With Answer
Working With Functions - Worksheet With Answer
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 ( )
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
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.