ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
lst = [1, 2, 3]
lst2 = lst
lst *= 2
lst2 += [9]
print(lst, lst2, lst is lst2)
  1. A. [1, 2, 3, 1, 2, 3, 9] [1, 2, 3, 1, 2, 3, 9] True
  2. B. [1, 2, 3, 1, 2, 3] [1, 2, 3, 1, 2, 3, 9] False
  3. C. [1, 2, 3, 9] [1, 2, 3, 9] True
  4. D. [1, 2, 3, 1, 2, 3, 9] [1, 2, 3, 1, 2, 3] True

Correct answer: A. [1, 2, 3, 1, 2, 3, 9] [1, 2, 3, 1, 2, 3, 9] True

*= and += on a list change the same object in place, so lst and lst2 are still one list. The last += is therefore visible through both names.

More Python Programming questions

Exams that ask this topic