ProgrammingVery hardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode? (Print writes its value followed by a space.)
What will be the output of the following pseudocode? (Print writes its value followed by a space.)
Function f(n)
If n <= 0 Then Return 0
Print n
r = f(n - 2) + f(n - 3)
Print n
Return r + 1
End Function
Print f(5)- A. 4 5 3 1 1 3 2 2 5
- B. 5 3 1 1 3 2 2 5 4
- C. 5 3 1 2 4
- D. 5 3 1 1 3 2 2 5 0
Correct answer: B. 5 3 1 1 3 2 2 5 4
The call tree is f(5) -> f(3) [-> f(1), f(0)] and f(2) [-> f(0), f(-1)]; each real call prints n before and after its children. Tracing the pre- and post-prints gives the sequence shown, and the final return value counts the calls with n > 0.
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? (Parameters marked ref are passed by reference; the others by value.) Procedure…
- What will be the output of the following pseudocode? (AND has higher precedence than OR, and && style short-circuit evaluation is used…
- What will be the output of the following pseudocode? (^ is bitwise XOR, & is bitwise AND, >> is a right shift; all values are non-negative…
- What will be the output of the following pseudocode? (break leaves only the innermost loop; continue skips to the next iteration of the…
- What will be the output of the following pseudocode? (Arrays are 0-indexed.) Integer a[6] = {4, 7, 1, 9, 3, 6} Integer i = 0 For k = 1 to…
- What will be the output of the following pseudocode? String s = "ABCDEF", r = "" For i = 0 to 5 If i mod 2 = 0 Then r = s[i] + r Else r =…