ProgrammingModerateGATE CS · Product-company placement

What is printed?

What is printed?
print([[r * c for c in range(r)] for r in range(1, 4)])
  1. A. [[0], [0, 2], [0, 3, 6]]
  2. B. [[1], [2, 4], [3, 6, 9]]
  3. C. [[], [0], [0, 2]]
  4. D. [[0, 0, 0], [0, 1, 2], [0, 2, 4]]

Correct answer: A. [[0], [0, 2], [0, 3, 6]]

The outer comprehension runs r = 1, 2, 3 and the inner one uses c in range(r), i.e. 0..r-1; so row r is [0, r, 2r, ...] with r items.

More Python Programming questions

Exams that ask this topic