ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
x = 255
print(x.bit_length(), bin(x).count('1'), (x & -x), (x - 1) & x)- A. 8 8 255 0
- B. 7 8 1 254
- C. 8 8 1 254
- D. 8 7 1 254
Correct answer: C. 8 8 1 254
255 is 11111111 in binary, so bit_length is 8 and it has eight 1-bits. x & -x isolates the lowest set bit, 1. (x - 1) & x clears that lowest bit, giving 254.
More Python Programming questions
- What is printed? python print(-7 // 2, 7 // -2, -7 % 2, 7 % -2)
- What is printed? python def pairs(a, t): a = sorted(a) i, j, c = 0, len(a) - 1, 0 while i < j: s = a[i] + a[j] if s == t: c += 1 i += 1 j…
- What is printed? python def countk(a, k): pre = {0: 1} s = 0 ans = 0 for x in a: s += x ans += pre.get(s - k, 0) pre[s] = pre.get(s, 0) +…
- What is printed? python from collections import deque g = ['..#', '.##', '...'] def bfs(g): R, C = len(g), len(g[0]) d = {(0, 0): 0} q =…
- What is printed? python from functools import lrucache calls = 0 @lrucache(maxsize=None) def f(n): global calls calls += 1 if n < 2…
- What is printed? python def greedy(coins, amt): cnt = 0 for c in sorted(coins, reverse=True): cnt += amt // c amt %= c return cnt if amt…