ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
print(len('%s' % 1.0), len('%g' % 1.0), len(repr(1.0)), str(1e5), '%g' % 1e5, '%g' % 1e6)- A. 3 1 1 100000.0 100000 1000000
- B. 3 1 3 100000 100000 1e+06
- C. 3 1 3 100000.0 100000 1e+06
- D. 3 1 3 100000.0 100000 1000000
Correct answer: C. 3 1 3 100000.0 100000 1e+06
%s and repr give 1.0, which is three characters. %g drops trailing zeros and switches to exponent form at a million, so 1e6 prints as 1e+06 while 1e5 prints as 100000. str(1e5) keeps the .0.
More Python Programming questions
- What is printed? python def f(s): return s[::-1] == s print(f('abba'), f('ab' 2), f(''), f('a'))
- What is printed? python print(True + True, str(True) + '1', str(None), str([None]), repr('None'))
- What is printed? python print(sorted([0.5, -0.0, 0.0, -1]))
- What is printed? python x = 10 16 print(float(x) == x, float(x + 1) == x, x + 1.0 == x, (x + 1) - x)
- What is printed? python print('%5s|%-5s|' % ('ab', 'cd'))
- What is printed? python print(len('%-8.3f|' % 3.14159), len('%08.3f' % -3.14159), '%08.3f' % -3.14159)