ProgrammingVery hardProduct company OA

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 <= 1)
        return n
    end if
    return B(n - 1) + 1
End Function
Function B(Integer n)
    if (n <= 1)
        return 1
    end if
    return A(n / 2) * 2
End Function
Print A(10), B(10)
  1. A. 7 6
  2. B. 7 10
  3. C. 6 7
  4. D. 9 10

Correct answer: B. 7 10

A(10) = B(9) + 1; B(9) = 2·A(4); A(4) = B(3) + 1; B(3) = 2·A(1) = 2 → A(4) = 3 → B(9) = 6 → A(10) = 7. B(10) = 2·A(5); A(5) = B(4) + 1; B(4) = 2·A(2); A(2) = B(1) + 1 = 2 → B(4) = 4 → A(5) = 5 → B(10) = 10. Output 7 10. Halving with n / 2 in B is what keeps the recursion short; '7 6' reuses B(9) = 6 as if it were B(10).

More Pseudocode and Code Tracing questions

Exams that ask this topic