1 Crore+ students have signed up on EduRev. Have you? Download the App |
Which of the following is NOT a characteristic of recursive functions?
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);
Which of the following problems can be solved using recursion?
What is the output of the following code?
#include <iostream>
void printNumbers(int n) {
if (n > 0) {
printNumbers(n - 1);
cout << n << " ";
}
}
printNumbers(5);
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);
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);
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, "");
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, "");
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);
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);
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);
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);
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);
153 videos|115 docs|24 tests
|
153 videos|115 docs|24 tests
|