ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
a = [1, 2, 3]
b = a
a += a
c = [1, 2]
c.append(c)
print(b, len(c), c[2][2][0], c)
  1. A. [1, 2, 3] 3 1 [1, 2, [...]]
  2. B. [1, 2, 3, 1, 2, 3] 3 1 RecursionError
  3. C. [1, 2, 3, 1, 2, 3] 3 1 [1, 2, [...]]
  4. D. [1, 2, 3, 1, 2, 3] 4 1 [1, 2, [1, 2]]

Correct answer: C. [1, 2, 3, 1, 2, 3] 3 1 [1, 2, [...]]

a += a extends the shared object with a copy of its own items. A list containing itself is legal; indexing c[2] returns c again, and repr shows the cycle as [...].

More Python Programming questions

Exams that ask this topic