ProgrammingHardAmazon OA · TCS Digital
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Function ways(Integer n)
if (n < 0)
return 0
end if
if (n == 0)
return 1
end if
return ways(n - 1) + ways(n - 2) + ways(n - 3)
End Function
Print ways(6)- A. 13
- B. 24
- C. 44
- D. 32
Correct answer: B. 24
ways(n) counts ways to climb n steps taking 1, 2 or 3 at a time: 1, 1, 2, 4, 7, 13, 24 for n = 0 to 6, so 24. With only 1- and 2-steps the answer would be 13.
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? (substring(s, i, k) is the k characters starting at index i; / is integer division; +…
- What will be the output of the following pseudocode? Function f(Integer n) if (n == 0) return 0 end if if (n == 1) return 1 end if return…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[8] = {2, 0, 1, 2, 1, 0, 2, 1} Integer lo = 0…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[7] = {7, 2, 9, 4, 1, 8, 5} Integer pivot =…
- What will be the output of the following pseudocode? Integer a[9] = {3, 3, 4, 2, 4, 4, 2, 4, 2} Integer cand = 0, cnt = 0 for (each i from…
- What will be the output of the following pseudocode? Integer a[7] = {16, 17, 4, 3, 5, 2, 1} Integer mx = a[6], c = 1 for (each i from 5…