ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def walk(s):
total = 0
for i, ch in enumerate(s, start=1):
if ch.isupper():
total += i
else:
total -= i
return total
print(walk('aBcD'))- A. 0
- B. 2
- C. -3
- D. -4
Correct answer: B. 2
Positions 1 to 4 are a, B, c, D. Uppercase B and D add 2 and 4, while a and c subtract 1 and 3. The total is 2 + 4 - 1 - 3 = 2.
More Python Programming questions
- What is printed? python print(sum(x for x in range(1, 11) if x & 1), sum(x for x in range(1, 11) if x & 2))
- What is printed? python x = 'AbC' print(x.swapcase(), x.lower() < x.upper(), sorted(x) == ['A', 'C', 'b'])
- What is printed? python print(max(range(5), key=lambda x: -abs(x - 2)), min(range(5), key=lambda x: abs(x - 2)))
- What is printed? python print(sum(divmod(17, 5)), divmod(-17, 5)[1], 17 % 5 - (-17) % 5)
- What is printed? python def prodexcept(a): n = len(a) out = [1] n left = 1 for i in range(n): out[i] = left left = a[i] right = 1 for i in…
- What is printed? python import bisect def span(a, x): lo = bisect.bisectleft(a, x) if lo == len(a) or a[lo] != x: return (-1, -1) return…