ProgrammingVery hardJEE Main · CAT placement
What is printed?
What is printed?
def f(n):
c = 0
while n:
n &= n - 1
c += 1
return c
print(f(255), f(-1 & 0xFF), f(0))- A. 8 7 0
- B. 8 8 0
- C. 9 8 0
- D. 8 8 1
Correct answer: B. 8 8 0
n &= n - 1 clears the lowest set bit on each pass, so the loop counts set bits. 255 has eight and -1 & 0xFF is also 255, while 0 never enters the loop.
More Python Programming questions
- What is printed? python s = 'abc' print(s[::-1] + s[0], s[1:] + s[:1], s[-1] + s[-2:])
- What is printed? python print(len('é'), len('é'.encode()), len(f'{"é":>3}'), len(str(0.1 + 0.2)))
- What is printed? python print('%-6s|' % 'ab', '%6.2f|' % 3.14159, '%+.1e' % 12345.6789)
- What is printed? python from decimal import Decimal, ROUNDHALFEVEN print(Decimal('2.5').quantize(Decimal('1'), rounding=ROUNDHALFEVEN)…
- What is printed? python x = 'a,b;c' print(x.split(',', 1), x.split(';')[0].split(','), [t for t in x.replace(';', ',').split(',') if t]…
- What is printed? python print(sorted(['10', '9', '100', '8'], key=len), sorted(['10', '9', '100', '8'], key=int))