ProgrammingHardGATE CS · Product-company placement

What is printed?

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

Correct answer: B. [7] [7, 8]

Slice assignment x[:] = [7] replaces the contents of the shared object (so y sees [7]); x = x + [8] then creates a new list bound only to x.

More Python Programming questions

Exams that ask this topic