ProgrammingModerateTCS NQT · Accenture
The procedure below uses exponentiation by squaring on the exponent 37. It counts one multiplication into result each time the current lowest bit of the exponent is 1. What is printed?
The procedure below uses exponentiation by squaring on the exponent 37. It counts one multiplication into result each time the current lowest bit of the exponent is 1. What is printed?
e = 37; count = 0
while e > 0
if e is odd then count = count + 1
e = e div 2
end while
Print count- A. 6
- B. 4
- C. 3
- D. 1
Correct answer: C. 3
37 is 100101 in binary, which has three 1 bits, so three multiplications go into result. Counting every loop pass gives 6, adding a stray 1 gives 4, and counting only the first multiplication gives 1.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- Recursive merge sort splits an array of length n into halves of length n div 2 and n minus n div 2, and calls itself on each half unless…
- The loop below multiplies i by 3 each time while i is less than 1000, starting from i = 1, and counts the iterations. What is printed? i =…
- A nested loop counts the pairs (i, j) with 1 <= j <= i <= 6 that also satisfy i + j even. The procedure prints the count. 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…
- 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…