Recursion in C is a process where a function calls itself directly or indirectly, allowing the function to repeat its behavior.
2. What is a base case in recursion?
a) The initial value for a recursive function
b) The condition under which the recursion stops
c) The maximum number of times a function can call itself
d) The main function in a recursive program
Answer:
b) The condition under which the recursion stops
Explanation:
The base case in a recursive function is a condition that stops the recursion by preventing further recursive calls.
3. Which of the following is essential for a recursive function to avoid infinite recursion?
a) A loop
b) A global variable
c) A base case
d) A static variable
Answer:
c) A base case
Explanation:
A base case is essential in a recursive function to terminate the recursion. Without it, the function would call itself indefinitely, leading to infinite recursion.
4. What is stack overflow in the context of recursive functions in C?
a) An error that occurs when the program runs out of memory
b) A situation where too many functions are called at once
c) An error that occurs when recursive calls exhaust the call stack
d) A condition where the stack data structure overflows
Answer:
c) An error that occurs when recursive calls exhaust the call stack
Explanation:
Stack overflow in recursion occurs when there are too many nested recursive calls, exhausting the available call stack memory.
5. Which of the following problems is typically solved using recursion?
a) Calculating the factorial of a number
b) Swapping two variables
c) Finding the maximum value in an array
d) Printing a string
Answer:
a) Calculating the factorial of a number
Explanation:
Calculating the factorial of a number is a classic example of a problem that is commonly solved using recursion in programming.
6. What is tail recursion in C programming?
a) A recursive function with no base case
b) A recursive function where the recursive call is the last operation
c) A recursive function that calls multiple functions
d) A recursive function without return statement
Answer:
b) A recursive function where the recursive call is the last operation
Explanation:
Tail recursion is a type of recursion where the recursive call is the last operation in the function. In some cases, compilers optimize tail recursion to improve performance.
7. Can a recursive function in C have more than one base case?
a) No, a recursive function can only have one base case
b) Yes, but only in special circumstances
c) Yes, a recursive function can have multiple base cases
d) Yes, but all base cases must be identical
Answer:
c) Yes, a recursive function can have multiple base cases
Explanation:
Recursive functions can have multiple base cases to handle different conditions for terminating the recursion.
8. What is indirect recursion in C?
a) A function that calls itself multiple times
b) A function that calls a sequence of other functions, one of which calls the first function
c) A function that does not have a direct call to itself
d) A recursive function without a base case
Answer:
b) A function that calls a sequence of other functions, one of which calls the first function
Explanation:
Indirect recursion occurs when a function calls another function, and that function (directly or indirectly) calls the first function again.
9. In a recursive function, which memory area is primarily used for storing function calls?
a) Heap
b) Stack
c) Global memory
d) Static memory
Answer:
b) Stack
Explanation:
In recursive functions, each function call is stored in the call stack, which keeps track of the point to which each active function should return control when it finishes executing.
10. How does recursion differ from iteration?
a) Recursion is faster than iteration
b) Recursion uses less memory than iteration
c) Recursion involves function calls, while iteration uses loops
d) Recursion can only solve simple problems
Answer:
c) Recursion involves function calls, while iteration uses loops
Explanation:
The key difference between recursion and iteration is that recursion involves repeated function calls, while iteration repeatedly executes a set of instructions using loops.