All Exams  >   Software Development  >   DSA in C++  >   All Questions

All questions of Recursion for Software Development Exam

Which of the following problems can be solved using recursion?
  • a)
    Finding the maximum element in an array
  • b)
    Sorting an array in ascending order
  • c)
    Calculating the Fibonacci sequence
  • d)
    Searching for an element in a linked list
Correct answer is option 'C'. Can you explain this answer?

Qudrat Chauhan answered
The Fibonacci sequence can be efficiently calculated using recursion. Each term in the sequence is the sum of the two preceding terms, and this pattern can be expressed recursively. Other problems mentioned, such as finding the maximum element in an array or sorting an array, can be solved more efficiently using other techniques like iteration.

Which of the following is NOT a characteristic of recursive functions?
  • a)
    They must have a base case.
  • b)
    They must have a recursive case.
  • c)
    They always involve the use of loops.
  • d)
    They can be used to solve complex problems.
Correct answer is option 'C'. Can you explain this answer?

Tanuja Mishra answered
Recursive functions do not always involve the use of loops. In fact, recursive functions are an alternative to iterative loops. Recursion involves calling the same function within itself, allowing the problem to be solved by dividing it into smaller subproblems.

What is the output of the following recursive function?
int factorial(int n) {
    if (n == 0) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}
cout << factorial(5);
  • a)
    5
  • b)
    25
  • c)
    120
  • d)
    720
Correct answer is option 'C'. Can you explain this answer?

Ankit Kulkarni answered
The given code snippet is missing the `main` function that calls the `factorial` function and prints the output. Here is an example of how the code can be modified to include the `main` function and print the output:

```cpp
#include

int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int n = 5;
std::cout < factorial(n)="" />< />
return 0;
}
```

In this case, the output will be:

```
120
```

This is because `factorial(5)` is calculated as `5 * 4 * 3 * 2 * 1`, which equals `120`.

What is the output of the following code?
#include <iostream>
int sum(int n) {
    if (n == 0)
        return 0;
    
    return n + sum(n - 1);
}
int main() {
    std::cout << sum(4);
    return 0;
}
  • a)
    4
  • b)
    10
  • c)
    6
  • d)
    0
Correct answer is option 'C'. Can you explain this answer?

Manasa Iyer answered
Understanding the Code
The provided C++ code defines a recursive function `sum(int n)` that calculates the sum of all integers from `1` to `n`. Let's break it down step-by-step:

Function Definition
- The function `sum(int n)` checks if `n` is `0`.
- If `n` is `0`, it returns `0`, effectively serving as the base case for recursion.
- If `n` is not `0`, it returns the value of `n` plus the result of `sum(n - 1)`.

Recursive Calculation
- When `sum(4)` is called:
- It computes `4 + sum(3)`
- Then, `sum(3)` computes `3 + sum(2)`
- Next, `sum(2)` computes `2 + sum(1)`
- Finally, `sum(1)` computes `1 + sum(0)`
- `sum(0)` returns `0`, ending the recursion.

Step-by-Step Breakdown
- The calculations unfold as:
- `sum(1)` = `1 + 0` = `1`
- `sum(2)` = `2 + 1` = `3`
- `sum(3)` = `3 + 3` = `6`
- `sum(4)` = `4 + 6` = `10`

Final Output
- The final output of `std::cout < sum(4);`="" is="" `10`,="" which="" is="" the="" sum="" of="" the="" first="" four="" natural="" numbers="" (1="" +="" 2="" +="" 3="" +="" />

Correct Answer
- The correct answer is option **b) 10**.
This explanation clarifies how the recursive function computes the sum and confirms the correct output.

Which of the following is true about recursion?
  • a)
    Recursion is always more efficient than iteration.
  • b)
    Recursion uses more memory than iteration.
  • c)
    Recursion is always easier to understand than iteration.
  • d)
    Recursion can only be used with certain data structures.
Correct answer is option 'B'. Can you explain this answer?

Tanishq Das answered
Understanding Recursion and Memory Usage
Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. While it can be powerful and elegant, it typically uses more memory than iteration for several reasons:
Memory Consumption in Recursion
- Function Call Stack: Each recursive call adds a new layer to the call stack, which consumes additional memory. This stack keeps track of each function's state, including local variables and the point of execution.
- Stack Depth: In deep recursion, particularly with large inputs, the stack can grow significantly. If the recursion depth exceeds the stack limit, it can lead to stack overflow errors.
Comparison with Iteration
- Iteration: In contrast, iterative solutions use a single stack frame for the loop, consuming significantly less memory. The state is maintained within a few variables rather than multiple function calls.
- Efficiency: Recursion is not inherently more efficient than iteration. In fact, for many problems, iterative solutions can be faster and more memory-efficient.
Other Considerations
- Readability: While recursion can make some algorithms, like those for tree traversal, easier to understand, it is not universally clearer than iteration. This depends on the complexity of the problem and the programmer's familiarity with recursion.
- Data Structures: Recursion can be applied to various data structures, including trees and graphs, but it is not limited to specific types. It can be used with arrays and lists as well.
In summary, option 'B' is correct because recursion typically uses more memory than iteration due to the nature of the call stack in recursive function calls.

What is the output of the following code?
#include <iostream>
bool isPalindrome(string str, int start, int end) {
    if (start >= end) {
        return true;
    } else if (str[start] != str[end]) {
        return false;
    } else {
        return isPalindrome(str, start + 1, end - 1);
    }
}
string str = "racecar";
cout << isPalindrome(str, 0, str.length() - 1);
  • a)
    true
  • b)
    false
  • c)
    0
  • d)
    1
Correct answer is option 'A'. Can you explain this answer?

Codebreakers answered
The isPalindrome function checks if a string is a palindrome using recursion. It compares the characters at the start and end indices of the string, recursively moving towards the center of the string. If the characters are not equal, it returns false. If the start index becomes greater than or equal to the end index, it means all characters have been compared and found to be equal, so it returns true. Therefore, the output of isPalindrome("racecar", 0, str.length() - 1) would be true.

What is the output of the following code?
#include <iostream>
int countPairs(int n) {
    if (n == 0 || n == 1 || n == 2) {
        return 0;
    } else {
        return n - 1 + countPairs(n - 1);
    }
}
cout << countPairs(5);
  • a)
    10
  • b)
    4
  • c)
    6
  • d)
    3
Correct answer is option 'A'. Can you explain this answer?

TeamUnknown answered
The countPairs function calculates the number of pairs that can be formed by a given number of people using recursion. It subtracts 1 from the number of people and recursively calls itself with the reduced number until the base case is reached. The base case is when the number of people becomes 0, 1, or 2, in which case the count is 0. Therefore, the output of countPairs(5) would be 4 + 3 + 2 + 1 = 10.

What is the output of the following code?
#include <iostream>
void printSubsetSum(int arr[], int size, int index, int sum, string subset) {
    if (index == size) {
        if (sum == 0) {
            cout << subset << " ";
        }
        return;
    }
    
    printSubsetSum(arr, size, index + 1, sum - arr[index], subset + to_string(arr[index]) + " ");
    printSubsetSum(arr, size, index + 1, sum, subset);
}
int arr[] = {1, 2, 3};
printSubsetSum(arr, 3, 0, 3, "");
  • a)
    1 2
  • b)
    1 2 3
  • c)
    1 2 3 1 2
  • d)
    1 2 3 1 2 3
Correct answer is option 'B'. Can you explain this answer?

KnowIT answered
The printSubsetSum function prints all subsets of an array that sum up to a given target sum. It uses recursion to generate subsets by either including or excluding the current element at each step. The base case is when index is equal to size and sum is 0, in which case the current subset is printed. The function makes two recursive calls: one including the current element and subtracting its value from the sum, and another excluding the current element. Therefore, the output of printSubsetSum(arr, 3, 3, "") would be "1 2 3".

What is the output of the following code?
#include <iostream>
int countSubsetSum(int arr[], int size, int sum) {
    if (sum == 0) {
        return 1;
    } else if (size == 0) {
        return 0;
    } else if (arr[size - 1] > sum) {
        return countSubsetSum(arr, size - 1, sum);
    } else {
        return countSubsetSum(arr, size - 1, sum) + countSubsetSum(arr, size - 1, sum - arr[size - 1]);
    }
}
int arr[] = {1, 2, 3};
cout << countSubsetSum(arr, 3, 3);
  • a)
    2
  • b)
    3
  • c)
    4
  • d)
    5
Correct answer is option 'C'. Can you explain this answer?

Hackers World answered
The countSubsetSum function counts the number of subsets of an array that sum up to a given target sum using recursion. It makes two recursive calls: one including the current element and subtracting its value from the sum, and another excluding the current element. The base cases are when the sum becomes 0, in which case 1 is returned, or when the size becomes 0, in which case 0 is returned. Therefore, the output of countSubsetSum(arr, 3, 3) would be 4.

What is the output of the following code?
#include <iostream>
int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}
cout << gcd(24, 36);
  • a)
    6
  • b)
    12
  • c)
    18
  • d)
    24
Correct answer is option 'A'. Can you explain this answer?

Diksha Sharma answered
The gcd function calculates the greatest common divisor (GCD) of two numbers using recursion and the Euclidean algorithm. It divides a by b and recursively calls itself with b and the remainder of a divided by b until the base case is reached. The base case is when b becomes 0, in which case a is returned as the GCD. Therefore, the output of gcd(24, 36) would be the GCD of 24 and 36, which is 6.

In recursion, what is the base case?
  • a)
    The case where the problem is divided into smaller subproblems
  • b)
    The case where the problem is solved without further recursion
  • c)
    The case where the problem cannot be solved recursively
  • d)
    The case where the recursion depth reaches its maximum limit
Correct answer is option 'B'. Can you explain this answer?

Tanuja Mishra answered
The base case is a condition in a recursive function that specifies when the recursion should stop. It is the case where the problem is solved directly without further recursion. Without a base case, the recursive function would continue calling itself indefinitely, resulting in infinite recursion.

What is the output of the following code?
#include <iostream>
int power(int base, int exponent) {
    if (exponent == 0) {
        return 1;
    } else {
        return base * power(base, exponent - 1);
    }
}
cout << power(2, 4);
  • a)
    2
  • b)
    4
  • c)
    8
  • d)
    16
Correct answer is option 'C'. Can you explain this answer?

Codebreakers answered
The power function calculates the exponentiation of a base number. It uses recursion to multiply the base by itself exponent number of times. The base case is when exponent is 0, in which case 1 is returned. Otherwise, the result is calculated as base multiplied by power(base, exponent - 1). Therefore, power(2, 4) would be equal to 2 * 2 * 2 * 2 = 16.

What is the output of the following code?
#include <iostream>
int sumDigits(int n) {
    if (n == 0) {
        return 0;
    } else {
        return n % 10 + sumDigits(n / 10);
    }
}
cout << sumDigits(12345);
  • a)
    15
  • b)
    10
  • c)
    14
  • d)
    25
Correct answer is option 'A'. Can you explain this answer?

TeamUnknown answered
The sumDigits function calculates the sum of digits of a given number using recursion. It adds the last digit of the number to the sum of the remaining digits, which is obtained by dividing the number by 10. The base case is when the number becomes 0, in which case the sum is 0. Therefore, the output of sumDigits(12345) would be 1 + 2 + 3 + 4 + 5 = 15.

What is the output of the following code?
#include <iostream>
void printSubset(int arr[], int size, int index, string subset) {
    if (index == size) {
        cout << subset << " ";
        return;
    }
    
    printSubset(arr, size, index + 1, subset + to_string(arr[index]) + " ");
    printSubset(arr, size, index + 1, subset);
}
int arr[] = {1, 2, 3};
printSubset(arr, 3, 0, "");
  • a)
    1 2 3
  • b)
    1 1 1 2 3 2 2 3 3
  • c)
    1 2 3 1 2 1 3 2 3
  • d)
    1 2 3 1 2 3
Correct answer is option 'D'. Can you explain this answer?

KnowIT answered
The printSubset function prints all possible subsets of an array. It uses recursion to generate subsets by either including or excluding the current element at each step. The base case is when index is equal to size, in which case the current subset is printed. The function makes two recursive calls: one including the current element and another excluding it. Therefore, the output of printSubset(arr, 3, 0, "") would be "1 2 3 1 2 3".

What is the output of the following code?
#include <iostream>
int fibonacci(int n) {
    if (n <= 1) {
        return n;
    } else {
        return fibonacci(n - 1) + fibonacci(n - 2);
    }
}
cout << fibonacci(5);
  • a)
    3
  • b)
    5
  • c)
    8
  • d)
    13
Correct answer is option 'C'. Can you explain this answer?

Diksha Sharma answered
The fibonacci function calculates the nth Fibonacci number using recursion. It uses the property that each Fibonacci number is the sum of the two preceding numbers. The base case is when n is less than or equal to 1, in which case n is returned. Therefore, the output of fibonacci(5) would be the 5th Fibonacci number, which is 8.

What is the output of the following code?
#include <iostream>
void printNumbers(int n) {
    if (n > 0) {
        printNumbers(n - 1);
        cout << n << " ";
    }
}
printNumbers(5);
  • a)
    5 4 3 2 1
  • b)
    1 2 3 4 5
  • c)
    1 1 1 1 1
  • d)
    0 1 2 3 4
Correct answer is option 'A'. Can you explain this answer?

Codebreakers answered
The printNumbers function prints the numbers from n to 1 in descending order. It uses recursion to print the numbers in reverse order by first recursively calling itself with n - 1, and then printing n. The base case is when n is greater than 0. Therefore, the output of printNumbers(5) would be "5 4 3 2 1".

Chapter doubts & questions for Recursion - DSA in C++ 2025 is part of Software Development exam preparation. The chapters have been prepared according to the Software Development exam syllabus. The Chapter doubts & questions, notes, tests & MCQs are made for Software Development 2025 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests here.

Chapter doubts & questions of Recursion - DSA in C++ in English & Hindi are available as part of Software Development exam. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free.

DSA in C++

152 videos|115 docs|24 tests

Top Courses Software Development