ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
total = 0
for s in ['10', '9', ' x', '+7', '1_0']:
try:
total += int(s)
except ValueError:
total -= 1
print(total)- A. 34
- B. 36
- C. 35
- D. 25
Correct answer: C. 35
int accepts a leading plus sign and underscores between digits, and it ignores surrounding whitespace. Only ' x' fails, which subtracts 1, so 10 + 9 - 1 + 7 + 10 gives 35.
More Python Programming questions
- What is printed? python print('%d' % 3.9, '%d' % -3.9, '%.0f' % 0.5, '%i' % True)
- What is printed? python s = 'héllo' print(s[1], s.encode('latin-1')[1], len(s.encode('utf-16-le')), s.isascii())
- What is printed? python from decimal import Decimal, getcontext getcontext().prec = 4 print(Decimal(1) / Decimal(3), Decimal('1.10') ==…
- What is printed? python print(sorted([(2, 'b'), (1, 'z'), (2, 'a')], reverse=True))
- What is printed? python print(7 / 2 // 1, 7 // 2 / 1, -7 / 2 // 1, 7 % 2.5)
- What is printed? python print('abc'.find(''), 'abc'.count(''), ''.split(), ''.split(','))