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)- A. 20
- B. 26
- C. 22
- 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.
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? (g is a global variable.) Integer g = 0 Function f(Integer n) if (n == 0) return 0…
- What will be the output of the following pseudocode? (Integers are passed by value. An array parameter receives a reference to the…
- What will be the output of the following pseudocode? (&& binds more tightly than ||, and both stop evaluating as soon as the result is…
- What will be the output of the following pseudocode? Integer c = 0, i for (i = 1; i <= 50; i = i + 1) i = i 2 c = c + 1 end for Print c, i
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer a[8] = {1, 2, 3, 4, 6, 7, 8, 9} Integer i = 0, j…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; a 'from a down to b' loop does nothing when a < b.)…