ProgrammingHardProduct Company SDE · Python internals

What does the following Python 3 code print? a = [1, 2, 3] b = a b.append(4) print(a)

  1. A. [4, 1, 2, 3]
  2. B. Error
  3. C. [1, 2, 3, 4]
  4. D. [1, 2, 3]

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

b = a does not copy the list; both names reference the same object, so appending through b also changes what a sees.

More Python Programming questions

Exams that ask this topic