ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
a = [1, 2, 3]
for i, v in enumerate(a):
    a[i] = v * 2
    if i == 0:
        a.append(9)
    if len(a) > 4:
        break
print(a)
  1. A. [2, 4, 6, 18]
  2. B. [2, 4, 6, 9]
  3. C. [2, 4, 6, 18, 9]
  4. D. [2, 2, 3, 9]

Correct answer: A. [2, 4, 6, 18]

Iterating a list uses a live index, so the appended 9 is visited too and doubled to 18; the list length never exceeds 4, so the loop finishes normally.

More Python Programming questions

Exams that ask this topic