ProgrammingVery hardProduct company OA
What is printed?
What is printed?
x = -13
print(x >> 2, x // 4, ~x, x & 0xF, bin(x & 0xFF).count("1"), (-x) >> 2)- A. -3 -4 12 13 6 3
- B. -4 -4 -14 3 6 3
- C. -4 -4 12 3 6 3
- D. -4 -3 12 3 4 3
Correct answer: C. -4 -4 12 3 6 3
Python ints behave like infinite two's complement. >> is a floor shift: −13 >> 2 = ⌊−3.25⌋ = −4, the same as −13 // 4. ~x = −x − 1 = 12. −13 is …1111 0011, so the low nibble is 0011 = 3 and the low byte 1111 0011 has six 1-bits. 13 >> 2 = 3. The options with −3 truncate toward zero; 13 for x & 0xF masks the magnitude instead of the two's-complement pattern.
More Python Programming questions
- What is printed? python def chain(nums, divisors): g = (n for n in nums) for d in divisors: g = (n for n in g if n % d) return list(g)…
- What is printed? python class V: def init(self, n, log): self.n, self.log = n, log def add(self, o): self.log.append("add"); return…
- What is printed? python log = [] def t(x): log.append(x); return x def f(a, b=t("d1"), args, c=t("d2"), kw): return a, b, args, c…
- What is printed? python d = dict.fromkeys("ab", []) d["a"].append(1) d["b"] = d["b"] + [2] d["a"] += [3] g = [[]] 2 + [[] for in range(2)]…
- A log pipeline must remove duplicate event ids from a list xs of n = 2·10⁵ integers while keeping the order of first occurrence. Four…
- What is printed? python class Base: registry = [] def initsubclass(cls, kw): cls.registry.append(cls.name) cls.tag = kw.get("tag"…