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
  1. A. 6
  2. B. 4
  3. C. 3
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic