ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function fun(Integer n)
if (n > 0)
fun(n - 1)
Print n
fun(n - 1)
end if
End Function
fun(3)- A. 1 1 2 1 1 3
- B. 1 2 3 2 1
- C. 3 2 1 1 2 3
- D. 1 2 1 3 1 2 1
Correct answer: D. 1 2 1 3 1 2 1
fun(1) prints 1. fun(2) prints fun(1), 2, fun(1) = 1 2 1. fun(3) prints fun(2), 3, fun(2) = 1 2 1 3 1 2 1, which is 2³ - 1 = 7 numbers (the Tower of Hanoi pattern).
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? Integer a = 1, b = 1, c for (each i from 1 to 6) c = a + b a = b b = c end for Print b
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 100, c = 0 while…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer calls = 0 Function…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[5] = {3, 1, 4, 1, 5} for (each i from 1 to…
- What will be the output of the following pseudocode? Integer x = 0 for (each i from 1 to 100) if (i mod 4 == 0) continue end if if (i >…
- What will be the output of the following pseudocode? Integer a = 10, b = 3, c = 0 while (a >= b) a = a - b c = c + 1 end while Print c, a