Test Index
Functions in Python
© 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 =30def call (x) : global a if a%2==0: x+=a else: x-=a return xx=20print(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
Go to Question: