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

All questions of Recursion for Software Development Exam

1 Crore+ students have signed up on EduRev. Have you? Download the App

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?

Nilotpal Jain answered
Characteristics of Recursive Functions

Base Case:
- Recursive functions must have a base case, which is a condition that stops the recursion and prevents infinite loops.
- The base case defines the simplest scenario where the function does not call itself again.

Recursive Case:
- Recursive functions must also have a recursive case, where the function calls itself with modified input parameters.
- This allows the function to break down complex problems into smaller, more manageable subproblems.

Complex Problem Solving:
- Recursive functions are often used to solve complex problems by breaking them down into simpler subproblems.
- This divide-and-conquer approach can be more intuitive and easier to implement for certain types of problems.

Not Involving Loops:
- Contrary to common belief, recursive functions do not always involve the use of loops.
- Recursive functions achieve iteration through repeated function calls instead of explicit loops.
In conclusion, while recursive functions must have a base case, a recursive case, and can be used to solve complex problems, they do not necessarily involve the use of loops.

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?

Qudrat Chauhan answered
The given code calculates the factorial of a number using recursion. The base case is when n is 0, in which case 1 is returned. Otherwise, the factorial is calculated as n multiplied by the factorial of n - 1. Therefore, factorial(5) would be equal to 5 * 4 * 3 * 2 * 1 = 120.

What happens if a recursive function does not have a base case?
  • a)
    The program crashes with a stack overflow error.
  • b)
    The program goes into an infinite loop.
  • c)
    The function returns an error code.
  • d)
    The compiler throws a syntax error.
Correct answer is option 'B'. Can you explain this answer?

Harshad Khanna answered
Introduction:
In computer programming, a recursive function is a function that calls itself within its own definition. It is a powerful technique used to solve complex problems by breaking them down into smaller subproblems. However, when implementing a recursive function, it is crucial to include a base case, which is a condition that terminates the recursion. Without a base case, the recursive function will continue to call itself indefinitely, resulting in an infinite loop.

Explanation:
When a recursive function does not have a base case, the following consequences occur:

1. The program goes into an infinite loop:
Without a base case, the recursive function will continue to call itself repeatedly. Each recursive call creates a new instance of the function on the call stack, consuming memory. As a result, the program will eventually run out of available stack memory, causing a stack overflow error. This error occurs when the call stack exceeds its maximum size, leading to the termination of the program.

Example:
Consider the following example of a recursive function without a base case:

```
void infiniteLoop() {
infiniteLoop(); // Recursive call without base case
}

int main() {
infiniteLoop();
return 0;
}
```

In this example, the `infiniteLoop()` function calls itself indefinitely without any condition to terminate the recursion. As a result, the program will go into an infinite loop, continuously creating new instances of the function until the stack overflows.

Conclusion:
In summary, when a recursive function lacks a base case, it results in an infinite loop. This infinite loop consumes memory and can eventually lead to a stack overflow error, causing the program to crash. Therefore, it is crucial to include a base case in recursive functions to ensure termination and prevent unintended consequences.

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 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>
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>
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 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.

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".

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.

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.

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>
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.

Chapter doubts & questions for Recursion - DSA in C++ 2024 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 2024 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++

153 videos|115 docs|24 tests

Top Courses Software Development

Signup to see your scores go up within 7 days!

Study with 1000+ FREE Docs, Videos & Tests
10M+ students study on EduRev