ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
x = 0
for i in range(5):
x += i if i % 2 else -i
print(x)- A. -4
- B. 3
- C. 5
- D. -2
Correct answer: D. -2
The conditional expression is i if i is odd and -i if it is even. The contributions are 0, 1, -2, 3, -4, which sum to -2.
More Python Programming questions
- What is printed? python it = iter([3, 1, 0, 5, 0]) out = [] for x in iter(lambda: next(it), 0): out.append(x) print(out, list(it))
- What is printed? python s = 'key=value=end' print(s.partition('='), s.rpartition('='), 'a b'.split(' '))
- What is printed? python def shift(s, k): return ''.join(chr((ord(c) - 97 + k) % 26 + 97) if c.islower() else c for c in s)…
- What is printed? python def closest3(a, t): a = sorted(a) best = None for i in range(len(a) - 2): l, r = i + 1, len(a) - 1 while l < r: s…
- What is printed? python print(0.0 == -0.0, -0.0, 1e16 + 1 == 1e16, 2 53 + 1 == 2 53)
- What is printed? python import bisect xs = [1, 4, 4, 9] i = bisect.bisectleft(xs, 4) xs.insert(i, 4) print(xs, bisect.bisectright(xs, 4)…