ProgrammingModerateTCS NQT · Wipro pattern

What is printed?

What is printed?
a = "abc"
b = a
a += "d"
print(a, b)
  1. A. abcd abcd
  2. B. abcd abc
  3. C. abc abcd
  4. D. abc abc

Correct answer: B. abcd abc

Strings are immutable. a += "d" builds a NEW string and rebinds a to it. b still points to the original "abc". Compare this with lists, where += changes the list in place.

More Python Programming questions

Exams that ask this topic