ProgrammingVery hardProduct company OA · code-tracing level

What will be the output of the following pseudocode? (/ is integer division.)

What will be the output of the following pseudocode? (/ is integer division.)
Function A(Integer n)
    if (n <= 0)
        return 1
    end if
    return B(n - 1) + n
End Function

Function B(Integer n)
    if (n <= 0)
        return 0
    end if
    return A(n / 2) * 2
End Function

Print A(10)
  1. A. 20
  2. B. 26
  3. C. 22
  4. D. 12

Correct answer: C. 22

A(10) = B(9) + 10; B(9) = 2 × A(4); A(4) = B(3) + 4; B(3) = 2 × A(1); A(1) = B(0) + 1 = 0 + 1 = 1. Back up: B(3) = 2, A(4) = 6, B(9) = 12, A(10) = 22. 12 is B(9) alone; 20 comes from taking A(1) = B(0) = 0 instead of B(0) + 1.

More Pseudocode and Code Tracing questions

Exams that ask this topic