ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
def digits_sum(n):
    return sum(int(c) for c in str(abs(n)))
print(digits_sum(-9075), digits_sum(10**20), digits_sum(0))
  1. A. 12 1 0
  2. B. 21 0 0
  3. C. 21 1 0
  4. D. 21 1 1

Correct answer: C. 21 1 0

abs removes the sign before the digits are read, so -9075 gives 9 + 0 + 7 + 5 = 21. 10**20 has a single 1 followed by zeros.

More Python Programming questions

Exams that ask this topic