ProgrammingHardProduct company OA · Infosys pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Procedure fun(Integer n)
if (n <= 0)
return
end if
fun(n - 2)
Print n
fun(n - 1)
End Procedure
fun(3)- A. 3 1 2 1
- B. 1 3 1 2
- C. 1 2 3
- D. 1 3 2 1
Correct answer: D. 1 3 2 1
fun(3) first calls fun(1): that calls fun(−1) (nothing), prints 1, calls fun(0) (nothing). Back in fun(3): print 3, then fun(2): fun(0) nothing, print 2, fun(1) prints 1. Output: 1 3 2 1. Printing before the first recursive call (preorder style) gives 3 1 2 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? (/ is integer division.) Integer a = 1, b = 100, steps = 0 while (a < b) a = a 2 b =…
- What will be the output of the following pseudocode? (calls counts every call, including the first; memo starts with every entry equal to…
- What will be the output of the following pseudocode? (/ is integer division.) Integer c = 0 for (n = 1 to 50) Integer d = 0 for (k = 1 to…
- What will be the output of the following pseudocode? (&, |, ^ are bitwise AND, OR, XOR; << is a left shift.) Integer x = 0 for (i = 0 to…
- What will be the output of the following pseudocode? ('ref' parameters are passed by reference; others by value.) Procedure p(ref Integer…
- 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…