ProgrammingHardTCS NQT · Accenture pattern
What will be the output of the following pseudocode (counting all subsets of a set of size n)?
What will be the output of the following pseudocode (counting all subsets of a set of size n)?
Function countSubsets(Integer n)
if (n == 0)
return 1
end if
return 2 * countSubsets(n - 1)
End Function
Print countSubsets(5)- A. 25
- B. 10
- C. 16
- D. 32
Correct answer: D. 32
Each element doubles the number of subsets (include it or not), so countSubsets(n) computes 2^n; for n=5, that is 2^5 = 32.
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 (counting set bits using Brian Kernighan's trick, for each value 0 to 7)? (& is…
- What will be the output of the following pseudocode (longest common prefix of a list of strings)? Function lcp(List strs) String prefix =…
- What will be the output of the following pseudocode (finding the kth smallest element by sorting)? (Arrays are indexed from 0.) Integer…
- What will be the output of the following pseudocode (number of ways to climb n stairs, taking 1 or 2 steps at a time)? Function…
- What will be the output of the following pseudocode (product of array except self, without using division)? (Arrays are indexed from 0.)…
- What will be the output of the following pseudocode (how many output rows a zigzag string conversion uses)? Function numRowsUsed(String s…