ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
s = "abc"
t = s
s += "d"
L = [1]
M = L
L += [2]
print(t, M)
  1. A. abc [1, 2]
  2. B. abcd [1, 2]
  3. C. abc [1]
  4. D. abcd [1]

Correct answer: A. abc [1, 2]

+= on an immutable str builds a new string and rebinds s, so t still sees 'abc'. += on a list calls __iadd__, which extends the same object that M refers to.

More Python Programming questions

Exams that ask this topic