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)
  1. A. 25
  2. B. 10
  3. C. 16
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic