ProgrammingModerateGATE CS & IT · Infosys
What is printed?
What is printed?
dp = [0] * 16
for i in range(1, 16):
dp[i] = dp[i >> 1] + (i & 1)
print(dp[13], sum(dp))- A. 3 28
- B. 3 32
- C. 4 32
- D. 3 30
Correct answer: B. 3 32
dp[i] = dp[i >> 1] + (i & 1) counts the set bits of i. 13 is 1101 with three ones, and the popcounts of 0 to 15 sum to 32.
More Python Programming questions
- What is printed? python print([format(i ^ (i >> 1), '02b') for i in range(4)])
- What is printed? python from functools import reduce import operator a = [4, 1, 2, 1, 2] print(reduce(operator.xor, a)…
- What is printed? python def spf(n): s = list(range(n + 1)) for i in range(2, int(n 0.5) + 1): if s[i] == i: for j in range(i i, n + 1, i)…
- What is printed? python def row(n): r = [1] for in range(n): r = [1] + [r[i] + r[i + 1] for i in range(len(r) - 1)] + [1] return r…
- What is printed? python def pairs(a, t): a = sorted(a) l, r = 0, len(a) - 1 out = [] while l < r: s = a[l] + a[r] if s == t…
- What is printed? python class N: def init(self, v, nxt=None): self.v, self.nxt = v, nxt def rev(h): prev = None while h: h.nxt, prev, h =…