ProgrammingHardCapgemini · Infosys pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function fact(Integer n)
if (n <= 1)
return 1
end if
return n * fact(n - 1)
End Function
Print fact(6)- A. 120
- B. 720
- C. 360
- D. 6
Correct answer: B. 720
fact unwinds as 6*fact(5), recursively multiplying down to the base case fact(1)=1, giving the full product 6*5*4*3*2*1 = 720.
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? (All variables are integers, so / is integer division.) Function rec(Integer n…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Function power(Integer a…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 17, i = 2 Boolean…
- What will be the output of the following pseudocode? (Arrays are indexed from 0.) Integer arr[8] = {3, 1, 4, 1, 5, 9, 2, 6} Integer n = 8…
- What will be the output of the following pseudocode? Function countdownSum(Integer n) if (n == 0) return 0 end if return n +…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division.) Integer n = 16, c = 0, i = n…