ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
s = 'abc'
t = s
s += 'd'
print(t, s, t is s, id(s) == id(t))
  1. A. abc abcd False False
  2. B. abcd abcd False False
  3. C. abc abcd True True
  4. D. abc abc False False

Correct answer: A. abc abcd False False

Strings are immutable, so += creates a new object and rebinds s. t still refers to the old string 'abc', so the two names no longer point to the same object.

More Python Programming questions

Exams that ask this topic