Software Development Exam  >  Software Development Tests  >  DSA in C++  >  Test: Recursion - 1 - Software Development MCQ

Test: Recursion - 1 - Software Development MCQ


Test Description

15 Questions MCQ Test DSA in C++ - Test: Recursion - 1

Test: Recursion - 1 for Software Development 2024 is part of DSA in C++ preparation. The Test: Recursion - 1 questions and answers have been prepared according to the Software Development exam syllabus.The Test: Recursion - 1 MCQs are made for Software Development 2024 Exam. Find important definitions, questions, notes, meanings, examples, exercises, MCQs and online tests for Test: Recursion - 1 below.
Solutions of Test: Recursion - 1 questions in English are available as part of our DSA in C++ for Software Development & Test: Recursion - 1 solutions in Hindi for DSA in C++ course. Download more important topics, notes, lectures and mock test series for Software Development Exam by signing up for free. Attempt Test: Recursion - 1 | 15 questions in 30 minutes | Mock test for Software Development preparation | Free important questions MCQ to study DSA in C++ for Software Development Exam | Download free PDF with solutions
Test: Recursion - 1 - Question 1

Which of the following best defines recursion?

Detailed Solution for Test: Recursion - 1 - Question 1

Recursion involves breaking a problem into smaller subproblems that are of the same type as the original problem. It allows the problem to be solved by solving smaller instances of the same problem.

Test: Recursion - 1 - Question 2

In recursion, what is the base case?

Detailed Solution for Test: Recursion - 1 - Question 2

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.

1 Crore+ students have signed up on EduRev. Have you? Download the App
Test: Recursion - 1 - Question 3

Which of the following is NOT a characteristic of recursive functions?

Detailed Solution for Test: Recursion - 1 - Question 3

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.

Test: Recursion - 1 - Question 4

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);

Detailed Solution for Test: Recursion - 1 - Question 4

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.

Test: Recursion - 1 - Question 5

Which of the following problems can be solved using recursion?

Detailed Solution for Test: Recursion - 1 - Question 5

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.

Test: Recursion - 1 - Question 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);

Detailed Solution for Test: Recursion - 1 - Question 6

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

Test: Recursion - 1 - Question 7

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);

Detailed Solution for Test: Recursion - 1 - Question 7

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.

Test: Recursion - 1 - Question 8

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);

Detailed Solution for Test: Recursion - 1 - Question 8

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.

Test: Recursion - 1 - Question 9

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, "");

Detailed Solution for Test: Recursion - 1 - Question 9

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

Test: Recursion - 1 - Question 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, "");

Detailed Solution for Test: Recursion - 1 - Question 10

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

Test: Recursion - 1 - Question 11

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);

Detailed Solution for Test: Recursion - 1 - Question 11

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.

Test: Recursion - 1 - Question 12

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);

Detailed Solution for Test: Recursion - 1 - Question 12

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.

Test: Recursion - 1 - Question 13

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);

Detailed Solution for Test: Recursion - 1 - Question 13

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.

Test: Recursion - 1 - Question 14

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);

Detailed Solution for Test: Recursion - 1 - Question 14

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.

Test: Recursion - 1 - Question 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);

Detailed Solution for Test: Recursion - 1 - Question 15

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.

153 videos|115 docs|24 tests
Information about Test: Recursion - 1 Page
In this test you can find the Exam questions for Test: Recursion - 1 solved & explained in the simplest way possible. Besides giving Questions and answers for Test: Recursion - 1, EduRev gives you an ample number of Online tests for practice

Top Courses for Software Development

153 videos|115 docs|24 tests
Download as PDF

Top Courses for Software Development