📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!

  • google app store apple app store
  • ✖

      Question

      What is the output of the following recursive function

      call func(3) ? int func ( int n) {            if (n == 0 ) return 1 ;     return n * func(n - 1 ); }
      A 1 Correct Answer Incorrect Answer
      B 3 Correct Answer Incorrect Answer
      C 6 Correct Answer Incorrect Answer
      D 9 Correct Answer Incorrect Answer
      E 27 Correct Answer Incorrect Answer

      Solution

      This function computes the factorial of n . Factorial is defined as n!=n×(n−1)× ⋯ ×1 . For n=3 :

      1. func(3) calls 3 * func(2) .
      2. func(2) calls 2 * func(1) .
      3. func(1) calls 1 * func(0) .
      4. func(0) returns 1 (base case).
      The calculation is: 3×2×1=6 Why Other Options Are Incorrect:
      • Option A (1): Only the base case returns 1 . This does not account for recursive multiplication.
      • Option B (3): This is the input but not the factorial result.
      • Option D (9): This result might confuse with 3² , which is unrelated to factorial.
      • Option E (27): This is 3^3 .

      Practice Next
      ask-question