Recursion Quiz
Recursion Quiz
Recursion Quiz
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What is the base case for 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.
printf(“%d “, n)
b.
return
c.
if(n == 0)
d.
my_recursive_function(n-1)
Feedback
Your answer is correct.
The correct answer is: if(n == 0)
Question 2
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Which of the following statements is true?
a.
Recursion uses less memory compared to iteration
b.
Iteration is always better and simpler than recursion
c.
Recursion is always better than iteration
d.
Recursion uses more memory compared to iteration
Feedback
Your answer is correct.
The correct answer is: Recursion uses more memory compared to iteration
Question 3
Correct
Mark 1.00 out of 1.00
Flag question
Question text
6
8
9
Feedback
The correct answer is: 9
Question 4
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Recursive definitions on most computers are eventually implemented using a run-time stack
and this implementation is done by the operating system.
Question 4Answer
True
False
Feedback
The correct answer is 'True'.
Question 5
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Consider the following function:
The operating system detects the infinite recursion because of the "repeated state".
Feedback
The correct answer is: The run-time stack overflows, halting the program.
Question 6
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Recursion is a method in which the solution of a problem depends on ____________
a.
Smaller instances of different problems
b.
Larger instances of different problems
c.
Smaller instances of the same problem
d.
Larger instances of the same problem
Feedback
Your answer is correct.
The correct answer is: Smaller instances of the same problem
Question 7
Correct
Mark 1.00 out of 1.00
Flag question
Question text
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 1 to 10
b.
Prints the numbers from 10 to 1
c.
Prints the numbers from 10 to 0
d.
Prints the numbers from 0 to 10
Feedback
Your answer is correct.
The correct answer is: Prints the numbers from 1 to 10
Question 8
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Consider the binarySearch() function below:
int binarySearch(int [] a, int x, int low, int high)
{ int t, k;
if(low > high) return( -1);
k = (low + high) / 2;
if(a[k] == x)return(k);
if(x<a[k])return(binarySearch(a,x,low,k-1);
else return(binarySearch(a,x,k+1,high);
}
Suppose the array a is given by the statement:
int [] a = {2,4,6,8,10,12,14, 16};
For the call binarySearch(a,7,2, 5), how many calls to this function will be made,
including the original call?
Question 8Select one:
2
Feedback
The correct answer is: 3
Question 9
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Select the statement that is most correct.
Question 9Select one:
Tail recursion is a special case of recursion in which the last operation of the function, the
tail call, is a recursive call.
Tail recursion is a special case of recursion in which the first operation of the function, is a
recursive call.
Feedback
The correct answer is: Tail recursion is a special case of recursion in which the last operation
of the function, the tail call, is a recursive call.
Question 10
Correct
Mark 1.00 out of 1.00
Flag question
Question text
fun(0);
fun(-1043);
fun(150);
fun(1043);
Feedback
The correct answer is: fun(-1043);
Question 11
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Question 11Select one:
The operating system detects the infinite recursion because of the "repeated state"
Feedback
The correct answer is: The run-time stack overflows, halting the program
Question 12
Correct
Mark 1.00 out of 1.00
Flag question
Question text
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(5);
return 0;
}
b.
543212345
c.
5
d.
54321
Feedback
Your answer is correct.
The correct answer is: 5 4 3 2 1
Question 13
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Select the statement that is most correct.
Question 13Select one:
A recursive method is a method that invokes itself directly or indirectly. For a recursive
method to terminate there must be one or more base cases.
For a recursive method to terminate there must be one or more limit conditions.
Feedback
The correct answer is: A recursive method is a method that invokes itself directly or
indirectly. For a recursive method to terminate there must be one or more base cases.
Question 14
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Consider the following function:
int fun(int a, int n)
{ if(n == 0) return(1);
int t =fun(a,n/2);
if(n%2==0) return(t*t);
else return(t*t*a);
}
For the call fun(3, 3), how many calls to fun will be made, including the original call?
Question 14Select one:
2
Feedback
The correct answer is: 3
Question 15
Correct
Mark 1.00 out of 1.00
Flag question
Question text
The data structure used to implement recursive function calls _____________
a.
Binary tree
b.
Linked list
c.
Stack
d.
Array
Feedback
Your answer is correct.
The correct answer is: Stack
Question 16
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Fill in blank to form a correct statement: "A recursive method is a method that invokes itself
directly or indirectly. For a recursive method to terminate there must be one or more
____________".
Question 16Select one:
steps
conditions
limit conditions
base cases
Feedback
The correct answer is: base cases
Question 17
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Suppose the h( n) function is defined on the set of integer numbers as below.
For the call h(3), how many calls to h will be made, including the original call?
int h(int n)
{if (n == 0 || n==1)
return(1);
else
return(h(n-1)+h(n-2));
}
Question 17Select one:
2
Feedback
The correct answer is: 5
Question 18
Correct
Mark 1.00 out of 1.00
Flag question
Question text
An algorithm that calls itself directly or indirectly is known as
Question 18Select one:
Traversal algorithm
Polish notation
Sub algorithm
Recursion
Feedback
The correct answer is: Recursion
Question 19
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What is the output of the following code?
#include<stdio.h>
int main()
{
int n;
n=f1(4);
printf("%d",n);
return(0);
}
int f1(int x)
{ int b;
if(x==1)
return 1;
else
b=x*f1(x-1);
return b;
}
a.
12
b.
10
c.
4
d.
24
Feedback
Your answer is correct.
The correct answer is: 24
Question 20
Correct
Mark 1.00 out of 1.00
Flag question
Question text
State True or False: "Recursion bears substantial overhead. Each time the program calls a
method, the system must assign space for all of the method's local variables and parameters. This
can consume considerable memory and requires extra time to manage the additional space".
Question 20Select one:
True
False
Feedback
The correct answer is: True
Question 21
Correct
Mark 1.00 out of 1.00
Flag question
Question text
123
321
21
12
Feedback
The correct answer is: 1 2
Question 22
Correct
Mark 1.00 out of 1.00
Flag question
Question text
In recursion, the condition for which the function will stop calling itself is ____________
Question 22Select one:
a.
Best case
b.
There is no such condition
c.
Base case
d.
Worst case
Feedback
Your answer is correct.
The correct answer is: Base case
Question 23
Correct
Mark 1.00 out of 1.00
Flag question
Question text
135
7531
753
531
Feedback
The correct answer is: 5 3 1
Question 24
Correct
Mark 1.00 out of 1.00
Flag question
Question text
312
321
311
31
Feedback
The correct answer is: 3 1 1
Question 25
Correct
Mark 1.00 out of 1.00
Flag question
Question text
102
112
121
201
Feedback
The correct answer is: 1 0 2
Question 26
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Suppose the h ( n ) function is defined on the set of integer numbers as below.
What is the value of h(1)?
14
10
Feedback
The correct answer is: 8
Question 27
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What will be the output of the following code?
#include <stdio.h>
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.
9
b.
123456789
c.
0
d.
10
Feedback
Your answer is correct.
The correct answer is: 9
Question 28
Correct
Mark 1.00 out of 1.00
Flag question
Question text
What happens when the code shown below is executed?
#include<stdio.h>
int main()
{
printf("Hello");
main();
return 0;
}
a.
Hello is printed once
b.
0 is returned
c.
Hello is not printed at all
d.
Hello is printed infinite number of times
Feedback
Your answer is correct.
The correct answer is: Hello is printed infinite number of times
Question 29
Correct
Mark 1.00 out of 1.00
Flag question
Question text
97531
13579
7531
9753
Feedback
The correct answer is: 9 7 5 3 1
Question 30
Correct
Mark 1.00 out of 1.00
Flag question
Question text
How many times is the recursive function called, when the following code is executed?
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.
12
b.
10
c.
11
d.
9
Feedback
Your answer is correct.
The correct answer is: 11
Question 31
Correct
Mark 1.00 out of 1.00
Flag question
Question text
Consider the following code snippet:
void my_recursive_function()
{
my_recursive_function();
}
int main()
{
my_recursive_function();
return 0;
}
What will happen when the above snippet is executed?
a.
The code will show a compile time error
b.
The code will be executed successfully and no output will be generated
c.
The code will run for some time and stop when the stack overflows
d.
The code will be executed successfully and random output will be generated
Feedback
Your answer is correct.
The correct answer is: The code will run for some time and stop when the stack overflows