ProgrammingHardCapgemini · Infosys pattern

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division.)
Function f(Integer n)
    if (n == 1)
        return 0
    end if
    return 1 + f(n / 2)
End Function

Print f(100)
  1. A. 50
  2. B. 6
  3. C. 5
  4. D. 7

Correct answer: B. 6

n goes 100 → 50 → 25 → 12 → 6 → 3 → 1: six halvings, so f(100) = 6 = floor(log₂ 100). Using the ceiling gives the trap 7.

More Pseudocode and Code Tracing questions

Exams that ask this topic