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)- A. 7 6
- B. 7 10
- C. 6 7
- 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).
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? (x is an 8-bit unsigned value; & is bitwise AND, | is OR, << and >> are shifts.)…
- What will be the output of the following pseudocode? (/ is integer division.) Integer x = 10 x = x + x 2 - 4 / 2 Print x
- What will be the output of the following pseudocode? for (i = 10; i >= 1; i = i - 3) Print i end for
- What will be the output of the following pseudocode? (/ is integer division; + on strings concatenates.) Integer n = 100 String s = ""…
- What will be the output of the following pseudocode? (/ is integer division.) Function f(Integer n) if (n == 0) return 0 end if return (n…
- What will be the output of the following pseudocode? (/ is integer division.) Integer n = 25, z = 0 while (n > 0) n = n / 5 z = z + n end…