Class - 12 CBSE Computer Science Important Output Questions 2025
Predict the output :
L1= [x ** 2 for x in range(10) if x % 3 == 0]
L2 = L1
L1.append(len(L1))
print(L1)
print(L2)
L2.remove(len(L2) - 1)
print(L1)
Python
Linear Lists
3 Likes
Answer
[0, 9, 36, 81, 4]
[0, 9, 36, 81, 4]
[0, 9, 36, 81]
Working
L1 = [x ** 2 for x in range(10) if x % 3 == 0]— This line creates a listL1containing the squares of multiples of 3 between 0 to 9.L2 = L1— This line assigns listL1to listL2, meaning bothL1andL2reference the same list in memory (shallow copy).L1.append(len(L1))— This line appends the length ofL1toL1. So, the length ofL1is 4, and it appends 4 toL1.print(L1)— This prints the modifiedL1list.print(L2)— This printsL2, which is pointing to the same list asL1.L2.remove(len(L2) - 1)— This line removes the last element ofL2sincelen(L2) - 1gives the last index ofL2.print(L1)— This printsL1again. Since bothL1andL2reference the same list, modifyingL2also affectsL1. Therefore,L1also reflects the change made toL2.
Answered By
3 Likes