ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
a = [3, 1, 4, 1, 5]
a.insert(-1, 9)
print(a, a.index(1), a.pop(), a.count(1))
  1. A. [3, 1, 4, 1, 9] 1 5 2
  2. B. [3, 1, 4, 1, 9, 5] 1 5 2
  3. C. [3, 1, 4, 1, 5, 9] 1 9 2
  4. D. [3, 1, 4, 1, 9, 5] 3 5 2

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

insert(-1, 9) puts 9 BEFORE the last element: [3, 1, 4, 1, 9, 5]. All four arguments are evaluated before print runs, and the first argument is the list object itself, not a snapshot. a.pop() removes 5 while the arguments are being built, so when print finally formats a it shows [3, 1, 4, 1, 9]. index(1) is the first 1 at index 1, and there are two 1s.

More Python Programming questions

Exams that ask this topic