ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
print([n for n in range(8) if n and n & (n - 1) == 0])
- A. [1, 2, 4]
- B. [0, 1, 2, 4]
- C. [1, 2, 4, 8]
- D. [2, 4]
Correct answer: A. [1, 2, 4]
n & (n - 1) is zero only when n has a single set bit. The range stops at 7, so 8 is not included, and 0 is excluded by the n test.
More Python Programming questions
- What is printed? python import heapq a = [5, 1, 4] heapq.heapify(a) print([heapq.heappop(a) for in range(len(a))], a)
- What is printed? python vals = [y for x in range(5) if (y := x x) % 2 == 0] print(vals)
- What is printed? python from functools import partial f = partial(int, base=2) print(f('101'), f('11', base=10))
- What is printed? python print(list(zip([(1, 'a'), (2, 'b')])))
- What is printed? python print(1 if 0 else 2 if 1 else 3, (lambda: 1 if False else 'x')())
- What is printed? python print(type(1/1).name, type(1//1).name, type(2-1).name, type(21).name)