1 Crore+ students have signed up on EduRev. Have you? Download the App |
What happens if a recursive function does not have a base case?
What is the output of the following code?
#include <iostream>
void printNumbers(int n) {
if (n <= 0)
return;
std::cout << n << " ";
printNumbers(n - 1);
}
int main() {
printNumbers(5);
return 0;
}
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;
}
What is the output of the following code?
#include <iostream>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
int main() {
std::cout << factorial(5);
return 0;
}
What is the output of the following code?
#include <iostream>
void printPattern(int n) {
if (n <= 0)
return;
std::cout << n << " ";
printPattern(n - 2);
std::cout << n << " ";
}
int main() {
printPattern(5);
return 0;
}
What is the output of the following code?
#include <iostream>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
std::cout << fibonacci(6);
return 0;
}
What does the following code do?
#include <iostream>
int countDigits(int n) {
if (n < 10)
return 1;
return 1 + countDigits(n / 10);
}
int main() {
std::cout << countDigits(12345);
return 0;
}
What does the following code do?
#include <iostream>
int power(int base, int exponent) {
if (exponent == 0)
return 1;
return base * power(base, exponent - 1);
}
int main() {
std::cout << power(2, 3);
return 0;
}
What does the following code do?
#include <iostream>
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
std::cout << gcd(18, 24);
return 0;
}
What does the following code do?
#include <iostream>
bool isPalindrome(const std::string& str, int start, int end) {
if (start >= end)
return true;
return (str[start] == str[end]) && isPalindrome(str, start + 1, end - 1);
}
int main() {
std::cout << isPalindrome("racecar", 0, 6);
return 0;
}
What does the following code do?
#include <iostream>
void printBinary(int n) {
if (n == 0)
return;
printBinary(n / 2);
std::cout << n % 2;
}
int main() {
printBinary(10);
return 0;
}