Test Index

Functions in Python Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 21 of 23
Marks: +1, -0
(a) Write the output of the code given below :
def short_sub (lst,n) :
for i in range (0,n) :
if len (lst)>4:
lst [i]=lst [i]+lst[i]
else:
lst[i]=lst[i]
subject=['CS','HINDI','PHYSICS','CHEMISTRY','MATHS']
short_sub(subject,5)
print(subject)
OR
(b) Write the output of the code given below :
a =30
def call (x) :
global a
if a%2==0:
x+=a
else:
x-=a
return x
x=20
print(call(35),end="#")
print(call(40),end= "@")
[CBSE 2023 Set 4]
Solution:  
(a)
The list subject is passed by reference, so changes made inside the function change the original list. len(lst) is 5, which is > 4 for every i from 0 to 4, so every element is joined to itself ('CS'+'CS' = 'CSCS').
Output:
['CSCS', 'HINDIHINDI', 'PHYSICSPHYSICS', 'CHEMISTRYCHEMISTRY', 'MATHSMATHS']
OR (b)
global a makes the function use the global a = 30. 30 % 2 == 0, so the if branch runs and x = x + 30 (the parameter x is local; the global x = 20 is not used).
call(35) = 35 + 30 = 65, printed with end="#"
call(40) = 40 + 30 = 70, printed with end="@"
Output:
65#70@
© examsnet.com
Advertisements
Go to Question: