Powered By Blogger

May 19, 2022

MFC-UNIT-1-3.Recursive Algorithms

 

Recursive Algorithms

1.        1

For a recursive algorithm _________

a.            a base case is necessary and is solved without recursion.

b.            a base case is not necessary

c.             doesnot solve a base case directly

d.            none of the mentioned

                Answer:a base case is not necessary

A

2.        2

1. Recursion is a method in which the solution of a problem depends on ____________

a) Larger instances of different problems

b) Larger instances of the same problem

c) Smaller instances of the same problem

d) Smaller instances of different problems

Answer: c

Explanation: In recursion, the solution of a problem depends on the solution of smaller instances of the same problem.

C

3.        3

Which of the following problems can’t be solved using recursion?

a) Factorial of a number

b) Nth fibonacci number

c) Length of a string

d) Problems without base case

 

Answer: d

Explanation: Problems without base case leads to infinite recursion call. In general, we will assume a base case to avoid infinite recursion call. Problems like finding Factorial of a number, Nth Fibonacci number and Length of a string can be solved using recursion.

D

4.        4

Recursion is similar to which of the following?

a) Switch Case

b) Loop

c) If-else

d) if elif else

 

Answer: b

Explanation: Recursion is similar to a loop.

B

5.        5

4. In recursion, the condition for which the function will stop calling itself is ____________

a) Best case

b) Worst case

c) Base case

d) There is no such condition

 

Answer: c

Explanation: For recursion to end at some point, there always has to be a condition for which the function will not call itself. This condition is known as base case.

C

6.        6

What is the output of the following code?

 

void my_recursive_function(int n)

{

    if(n == 0)

    return;

    printf("%d ",n);

    my_recursive_function(n-1);

}

int main()

{

    my_recursive_function(10);

    return 0;

}

 

a) 10

b) 1

c) 10 9 8 … 1 0

d) 10 9 8 … 1

 

Answer: d

Explanation: The program prints the numbers from 10 to 1.

 

7.        7

What is the output of the following code?

 

int cnt = 0;

void my_recursive_function(char *s, int i)

{

     if(s[i] == '\0')

        return;

     if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u')

     cnt++;

     my_recursive_function(s,i+1);

}

int main()

{

     my_recursive_function("thisisrecursion",0);

     printf("%d",cnt);

     return 0;

}

 

a) 6

b) 9

c) 5

d) 10

 

Answer: a

Explanation: The function counts the number of vowels in a string. In this case the number is vowels is 6.

A

8.        8

What will be the output of the following code?

 

int cnt=0;

void my_recursive_function(int n)

{

     if(n == 0)

     return;

     cnt++;

     my_recursive_function(n/10);

}

int main()

{

     my_recursive_function(123456789);

     printf("%d",cnt);

     return 0;

}

 

a) 123456789

b) 10

c) 0

d) 9

 

Answer: d

Explanation: The program prints the number of digits in the number 123456789, which is 9.

D

9.        9

Which of the following statements is true?

a) Recursion is always better than iteration

b) Recursion uses more memory compared to iteration

c) Recursion uses less memory compared to iteration

d) Iteration is always better and simpler than recursion

 

Answer: b

Explanation: Recursion uses more memory compared to iteration because every time the recursive function is called, the function call is stored in stack.

B

1.    10

What does the following recursive code do?

void my_recursive_function(int n)

{

     if(n == 0)

     return;

     my_recursive_function(n-1);

     printf("%d ",n);

}

int main()

{

     my_recursive_function(10);

     return 0;

}

a) Prints the numbers from 10 to 1
b) Prints the numbers from 10 to 0
c) Prints the numbers from 1 to 10
d) Prints the numbers from 0 to 10

Answer: c
Explanation: The above code prints the numbers from 1 to 10.

 

C

No comments:

Post a Comment

Featured Post

Data Analysis

    What is data analysis and its significance?   Data analysis is the process of collecting, transforming, and organizing data to dr...

Popular Posts