ProgrammingModerateTCS NQT · Infosys
What is printed on the second line?
What is printed on the second line?
u = ''
for ch in 'abc':
u = ch + u
print(u)- A. abc
- B. bca
- C. cba
- D. cab
Correct answer: C. cba
Each new character is put in front of the old text, so the final string is built from the last character backwards. Prepending copies the whole string each time, so the loop is O(n squared) in total.
More Python Programming questions
- What is printed on the first line? python x = 0.1 3 y = 0.3 print(abs(x - y) < 1e-9, x > y, x - y, y - x) print(float('0.1') 3 == 0.3, 3…
- What is printed on the second line? python import math print(math.isclose(1e9, 1e9 + 1e-3), math.isclose(1e9, 1e9 + 1, reltol=1e-9))…
- What is printed on the first line? python print(0.1 + 0.2 - 0.3 == 0, 0.1 + 0.2 - 0.3) print(1 - 0.9 == 0.1, 1 - 0.9)
- What is printed? python import sys print(sys.floatinfo.epsilon, 1.0 + sys.floatinfo.epsilon > 1.0, 1.0 + sys.floatinfo.epsilon / 2 > 1.0)
- What is printed on the first line? python print(263 2-63 == 1.0, 264 + 1.0 - 264, (264 + 1) - 264) print(float(264 + 1) - float(264))
- What is printed on the first line? python print(7 % -3, -7 % 3, 7 % 3, -7 % -3) print(-7 // -3, 7 // -3, -7 // 3)