ProgrammingHardCapgemini · Infosys SP

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 1
    end if
    return f(n / 2) + f(n / 2)
End Function
Print f(10), f(16)
  1. A. 8 16
  2. B. 10 16
  3. C. 16 16
  4. D. 8 32

Correct answer: A. 8 16

f(n) = 2 f(n/2) with f(1) = 1, so f(n) is the largest power of 2 not above n when halving: f(10) = 2 f(5) = 4 f(2) = 8 f(1) = 8, and f(16) = 16. Output 8 16.

More Pseudocode and Code Tracing questions

Exams that ask this topic