ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: C. 6 5 False

x + [5] creates a new list, so x no longer refers to y's list. y is the original list with 9 appended, which has 5 items. x is a new list with 6 items, and the two are not the same object.

More Python Programming questions

Exams that ask this topic