ProgrammingModerateTCS NQT · Wipro pattern

What is printed?

What is printed?
s = 'abc'
t = s
s += 'd'
u = list('abc')
v = u
u += ['d']
print(s, t, u is v, v)
  1. A. abcd abcd True ['a', 'b', 'c', 'd']
  2. B. abcd abc False ['a', 'b', 'c']
  3. C. abcd abc True ['a', 'b', 'c']
  4. D. abcd abc True ['a', 'b', 'c', 'd']

Correct answer: D. abcd abc True ['a', 'b', 'c', 'd']

Strings are immutable, so += creates a new string for s and leaves t alone. Lists are changed in place by +=, so u and v still refer to the same list.

More Python Programming questions

Exams that ask this topic