ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

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

Correct answer: D. 4 1

append(a) adds a reference to the list inside itself, so the list has length 4. a[-1] is the list itself, a[-1][-1] is the list again, and its first element is 1.

More Python Programming questions

Exams that ask this topic