Recursion as a Control Structure Quiz Quiz

Explore the fundamentals of recursion as a control structure with this engaging quiz. Assess your understanding of recursive reasoning, stack behavior, base and recursive cases, and identify common mistakes in recursive functions.

  1. Identifying Recursive Function Output

    Given the function 'int f(int n) { if (n == 0) return 1; else return n * f(n - 1); }', what value will be returned when calling f(4)?

    1. 24
    2. 0
    3. 4
    4. 5

    Explanation: This function computes the factorial of n; for f(4), it multiplies 4 * 3 * 2 * 1 = 24, making 24 the correct answer. Option 5 is a common mistake if someone adds instead of multiplies. Option 0 could be chosen if the implementation returned zero for n equals zero, which it does not. Option 4 confuses returning the input value rather than processing the recursion.

  2. Base Case Importance in Recursion

    Why is a base case essential in the design of any recursive function?

    1. It prevents infinite recursion and eventual stack overflow.
    2. It speeds up the execution of all programs.
    3. It helps to count the number of recursive calls.
    4. It controls variable scope automatically.

    Explanation: A base case ensures that the recursive function has a condition under which it stops calling itself, thus preventing infinite recursion and possible stack overflow errors. While efficient base cases can sometimes speed things up, speeding up all programs is incorrect. Base cases neither count recursive calls nor directly deal with variable scope.

  3. Stack Behavior During Recursion

    Consider a recursive function that prints a number, calls itself with the argument decremented, and then prints the number again. What is the expected order of output for input 2?

    1. 2, 2, 1, 1, 0, 0
    2. 2, 1, 0, 0, 1, 2
    3. 0, 1, 2, 2, 1, 0
    4. 0, 2, 1, 0, 1, 2

    Explanation: In this scenario, the function outputs before and after the recursive call, resulting in output as it descends (2, 1, 0) and as it ascends the call stack (0, 1, 2). The other options either reverse the correct order or repeat numbers incorrectly, showing a misunderstanding of the recursion stack or sequencing.

  4. Identifying a Common Recursive Error

    What will most likely happen if a recursive function lacks a condition in which it returns without calling itself (a base case)?

    1. The function will terminate after one call automatically.
    2. The program will eventually crash due to a stack overflow.
    3. The program will successfully compute the intended result.
    4. The compiler will automatically fix the code.

    Explanation: Without a base case, the function will continue calling itself indefinitely, eventually causing a stack overflow and crashing the program. Automatic termination after one call is incorrect because nothing stops the recursion. The program will not compute the intended result, and compilers do not add missing base cases.

  5. Direct vs. Indirect Recursion Examples

    Which of the following best illustrates indirect recursion?

    1. Function A prints “Hello” without any recursion.
    2. Function A does not call any other functions.
    3. Function A calls Function B, which then calls Function A.
    4. Function A calls itself directly without any intermediate functions.

    Explanation: Indirect recursion occurs when two or more functions call each other in a recursive cycle, such as Function A calling Function B, which then calls Function A. Direct recursion involves a function calling itself and does not include intermediaries. Functions that don't call others or only print without recursion are unrelated to indirect recursion.