ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
x = [1, 2, 3]
y = x
x = x + [4]
print(y, x, y is x)
  1. A. [1, 2, 3] [1, 2, 3, 4] False
  2. B. [1, 2, 3, 4] [1, 2, 3, 4] False
  3. C. [1, 2, 3] [1, 2, 3, 4] True
  4. D. [1, 2, 3, 4] [1, 2, 3, 4] True

Correct answer: A. [1, 2, 3] [1, 2, 3, 4] False

+ builds a new list, so x is rebound to that new list. y still refers to the original, which keeps three items, and the two names no longer share an object.

More Python Programming questions

Exams that ask this topic