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))- A. 12 1 0
- B. 21 0 0
- C. 21 1 0
- 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
- What is printed? python x = 1020 + 1 y = float(x) print(int(y) == x, int(y) - x, y == 1020, float(x) == y)
- What is printed? python import math print(math.floor(1e300) == 10300, math.floor(1e300) == int(1e300), round(1e300) == 10300)
- What is printed? python print(0.1 + 0.2 != 0.3, (0.1 + 0.2) - 0.3 < 1e-16, abs((0.1 + 0.2) - 0.3) <= 5e-17)
- What is printed? python print(max('zebra', 'Zebra'), min('a', 'B'), sorted('bA', key=ord)[0], 'bA' > 'Ab')
- What is printed? python print([1, 2] < [1, 2, 0], [1, 2] < [1, 3, 0], (1, 2) < (1, 2, 0), 'ab' < 'abc')
- What is printed? python print(hex(255), hex(-255), oct(-8), bin(0), int('-ff', 16), int('0xFF', 0))