ProgrammingVery hardTCS NQT · Accenture
The procedure below counts the numbers i from 1 to 100 whose binary form has exactly three 1 bits (bits are counted by repeatedly taking n mod 2 and dividing n by 2). What is printed?
The procedure below counts the numbers i from 1 to 100 whose binary form has exactly three 1 bits (bits are counted by repeatedly taking n mod 2 and dividing n by 2). What is printed?
count = 0
for i = 1 to 100
bits = number of 1 bits of i
if bits == 3 then count = count + 1
end for
Print count- A. 21
- B. 26
- C. 33
- D. 4
Correct answer: C. 33
Exactly 33 of the numbers 1 to 100 have three 1 bits. Counting those with two 1 bits gives 21, counting those with four 1 bits gives 26, and counting the bit lengths instead of the 1 bits gives 4 numbers with bit length 3.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- A string of parentheses "(()())((" is scanned by the procedure below. The variable open counts unmatched '(' at the end, and the procedure…
- The procedure below scans the string "abracadabra" from left to right and counts how many letters are repeats of an earlier letter (a…
- A string is a rotation of another when it can be obtained by moving some prefix to the end. The procedure below checks whether…
- The array [1, 2, 3, 4, 5, 6, 7] is shifted left by 2 positions by the procedure below: the element at index k is placed at index (k - 2)…
- The procedure below uses bitmasks over the set {1, 2, 3} (bit i set means element i+1 is included), so it visits the 8 subsets including…
- The number of trailing zeros in the decimal form of 2^20 5^3 is found by the procedure below, which divides out factors of 10 one at a…