ProgrammingModerateTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def tri(n):
    return n * (n + 1) // 2
print(tri(10), tri(0), [tri(k) for k in range(4)])
  1. A. 55 0 [1, 3, 6, 10]
  2. B. 55 0 [0, 1, 3, 6]
  3. C. 55 1 [0, 1, 3, 6]
  4. D. 45 0 [0, 1, 3, 6]

Correct answer: B. 55 0 [0, 1, 3, 6]

The formula n(n + 1)/2 is an integer for any integer n, and the // operator keeps it exact. For n = 10 the value is 55, and the first four values are 0, 1, 3 and 6.

More Python Programming questions

Exams that ask this topic