Q.(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)
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Part (a) -> ['CSCS', 'HINDIHINDI', 'PHYSICSPHYSICS', 'CHEMISTRYCHEMISTRY', 'MATHSMATHS']
Part (b) -> 65#70@
Part (a)
The function loops i from 0 to n-1 (here 0 to 4) and, on each pass, tests len(lst) > 4. The list subject starts with 5 elements, and no element is ever added or removed, so len(lst) stays 5 throughout. Because 5 > 4 is True for every iteration, the if branch always runs and doubles the string at that index by concatenating it with itself. (Note: the existing "the list is unchanged" reading is wrong — 5 IS greater than 4.)
subject[0]:'CS' + 'CS'->'CSCS'subject[1]:'HINDI' + 'HINDI'->'HINDIHINDI'subject[2]:'PHYSICS' + 'PHYSICS'->'PHYSICSPHYSICS'subject[3]:'CHEMISTRY' + 'CHEMISTRY'->'CHEMISTRYCHEMISTRY'subject[4]:'MATHS' + 'MATHS'->'MATHSMATHS'
Lists are mutable and passed by reference, so the change is visible after the call.
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)
Expected output:
['CSCS', 'HINDIHINDI', 'PHYSICSPHYSICS', 'CHEMISTRYCHEMISTRY', 'MATHSMATHS'] …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.