0% found this document useful (0 votes)
35 views11 pages

Python Functions Worksheet - Complete Solutions

ooooooooooooooooooooooooooo

Uploaded by

CHARVIK REDDY
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)
35 views11 pages

Python Functions Worksheet - Complete Solutions

ooooooooooooooooooooooooooo

Uploaded by

CHARVIK REDDY
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

PYTHON FUNCTIONS WORKSHEET - COMPLETE SOLUTIONS

1. Function name must be followed by


Ans: Parentheses ()

2. _____ keyword is used to define a function


Ans: def

3. Function will perform its action only when it is


Ans: Called/Invoked

4. Write statement to call the function.

python

def Add():
X = 10 + 20
print(X)
#statement to call the above function

Ans: Add()

5. Write statement to call the function.

python

def Add(X,Y):
Z = X+Y
print(Z)
#statement to call the above function

Ans: Add(10, 20) (or any two values)

6. Write statement to call the function.

python

def Add(X,Y):
Z = X+Y
return Z
#statement to call the above function
print("Total =",C)
Ans: C = Add(10, 20) (before the print statement)

7. Which Line Number Code will never execute?

python

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)
print(C)

Ans: Line 5 (print("Bye")) - because return statement terminates function execution

8. What will be the output of following code?

python

def Cube(n):
print(n*n*n)
Cube(10) # n is 10 here
print(Cube(10))

Ans:

1000
1000
None

9. What are the different types of actual arguments in function? Give example of
any one of them.
Ans: Types: Positional arguments, Keyword arguments, Default arguments, Variable-length arguments
Example of Positional argument: function(10, 20) where values are passed in order

10. What will be the output of following code:


python

def Alter(x, y = 10, z=20):


sum=x+y+z
print(sum)
Alter(10,20,30)
Alter(20,30)
Alter(100)

Ans:

60
70
130

11. Ravi a python programmer is working on a project, he has defined function as:

python

def CalculateInterest(Principal,Rate=.06,Time):
# code

Ans: Error: Non-default parameter 'Time' follows default parameter 'Rate' Solution: def
CalculateInterest(Principal, Time, Rate=.06):

12. Call the given function using KEYWORD ARGUMENT with values 100 and 200

python

def Swap(num1,num2):
num1,num2=num2,num1
print(num1,num2)
Swap( , )

Ans: Swap(num1=100, num2=200)

13. Which line number of code(s) will not work and why?
python

def Interest(P,R,T=7):
I = (P*R*T)/100
print(I)
Interest(20000,.08,15) #Line 1
Interest(T=10,20000,.075) #Line 2
Interest(50000,.07) #Line 3
Interest(P=10000,R=.06,Time=8) #Line 4
Interest(80000,T=10) #Line 5

Ans:

• Line 2: Non-keyword argument follows keyword argument

• Line 4: 'Time' is not a valid parameter name (should be 'T')

• Line 5: Missing required parameter 'R'

14. What will be the output of following code?

python

def Calculate(A,B,C):
return A*2, B*2, C*2
val = Calculate(10,12,14)
print(type(val))
print(val)

Ans:

<class 'tuple'>
(20, 24, 28)

15. What is Local Variable and Global Variables? Illustrate with example
Ans:

• Local Variable: Declared inside function, accessible only within that function

• Global Variable: Declared outside function, accessible throughout program


python

x = 100 # Global variable


def test():
y = 50 # Local variable
print(x, y) # Can access both
test()
print(x) # Can access global
# print(y) # Error - y is not accessible here

16. What will be the output of following code?

python

def check():
num=50
print(num)
num=100
print(num)
check()
print(num)

Ans:

100
50
100

17. What will be the output of following code?

python

def check():
global num
num=1000
print(num)
num=100
print(num)
check()
print(num)

Ans:
100
1000
1000

18. What will be the output of following code?

python

print("Welcome!")
print("Iam ", __name__ ) # is double underscore

Ans:

Welcome!
Iam __main__

19. Function can alter only Mutable data types? (True/False)


Ans: True

20. A Function can call another function or itself? (True/False)


Ans: True

21. What will be the output of following code?

python

def display(s):
l = len(s)
m=""
for i in range(0,l):
if s[i].isupper():
m=m+s[i].lower()
elif s[i].isalpha():
m=m+s[i].upper()
elif s[i].isdigit():
m=m+"$"
else:
m=m+"*"
print(m)
display("EXAM20@[Link]")

Ans: exam$*CBSE*COM
22. What will be the output of following code?

python

def Alter(M,N=50):
M=M+N
N=M-N
print(M,"@",N)
return M
A=200
B=100
A = Alter(A,B)
print(A,"#",B)
B = Alter(B)
print(A,"@",B)

Ans:

300 @ 200
300 # 100
150 @ 100
300 @ 150

23. What will be the output of following code?

python

def Total(Number=10):
Sum=0
for C in range(1,Number+1):
if C%2==0:
continue
Sum+=C
return Sum
print(Total(4))
print(Total(7))
print(Total())

Ans:

4
16
25
24. What will be the output of following code?

python

X = 100
def Change(P=10, Q=25):
global X
if P%6==0:
X+=100
else:
X+=50
Sum=P+Q+X
print(P,'#',Q,'$',Sum)
Change()
Change(18,50)
Change(30,100)

Ans:

10 # 25 $ 185
18 # 50 $ 318
30 # 100 $ 480

25. What will be the output of following code?

python

a=100
def show():
global a
a=200
def invoke():
global a
a=500
show()
invoke()
print(a)

Ans: 500

26. What will be the output of following code?


python

def drawline(char='$',time=5):
print(char*time)
drawline()
drawline('@',10)
drawline(65)
drawline(chr(65))

Ans:

$$$$$
@@@@@@@@@@
Error (int object can't be multiplied by string)
AAAAA

27. What will be the output of following code?

python

def Updater(A,B=5):
A = A // B
B=A%B
print(A,'$',B)
return A + B
A=100
B=30
A = Updater(A,B)
print(A,'#',B)
B = Updater(B)
print(A,'#',B)
A = Updater(A)
print(A,'$',B)

Ans:

3$3
6 # 30
6$1
7#7
1$1
2$7

28. What will be the output of following code?


python

def Fun1(num1):
num1*=2
num1 = Fun2(num1)
return num1
def Fun2(num1):
num1 = num1 // 2
return num1
n = 120
n = Fun1(n)
print(n)

Ans: 120

29. What will be the output of following code?

python

X = 50
def Alpha(num1):
global X
num1 += X
X += 20
num1 = Beta(num1)
return num1
def Beta(num1):
global X
num1 += X
X += 10
num1 = Gamma(num1)
return num1
def Gamma(num1):
X = 200
num1 += X
return num1
num = 100
num = Alpha(num)
print(num,X)

Ans: 420 80

30. What will be the output of following code?


python

def Fun1(mylist):
for i in range(len(mylist)):
if mylist[i]%2==0:
mylist[i]/=2
else:
mylist[i]*=2
list1 =[21,20,6,7,9,18,100,50,13]
Fun1(list1)
print(list1)

Ans: [42, 10.0, 3.0, 14, 18, 9.0, 50.0, 25.0, 26]

You might also like