ProgrammingVery hardAmazon OA · Goldman Sachs OA
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer cnt = 0
Procedure go(Integer i, Integer s)
if (s == 10)
cnt = cnt + 1
return
end if
if (i > 6 OR s > 10)
return
end if
go(i + 1, s + i)
go(i + 1, s)
End Procedure
go(1, 0)
Print cnt- A. 4
- B. 6
- C. 7
- D. 5
Correct answer: D. 5
go either takes i or skips it, so cnt counts subsets of {1, …, 6} with sum 10 (the check s == 10 comes first, so a subset is counted as soon as it reaches 10): {4, 6}, {1, 3, 6}, {1, 4, 5}, {2, 3, 5}, {1, 2, 3, 4}. cnt = 5.
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? Function G(Integer n) if (n == 0) return 0 end if return n - G(G(n - 1)) End Function…
- What will be the output of the following pseudocode? (code(c) gives a letter's character code; 'A' to 'Z' have consecutive codes) String w…
- What will be the output of the following pseudocode? (stop ends the program; arrays are indexed from 0) Integer a[6] = {3, 4, 5, 6, 1, 8}…
- What will be the output of the following pseudocode? (mod keeps the sign of the dividend (as in C); arrays are indexed from 0) Integer…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer calls = 0 Procedure…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[5] = {1, 2, 3, 4, 5}, comps = 0 Procedure…