Font size
WorksheetsC++ Recursion
Total questions: 12
Worksheet time: 24mins
The process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself is known as:
repetition
recursion
reapparition
reversing
Which of the following is the best definition of a recursive method?
A method that iterates itself exactly 5 times.
A method that invokes itself by name within the method.
A method that will never iterate infinitely.
A method that cannot be called more than once.
Recursion is similar to which of the following?
if-else
switch-case
loops
none of the above
Name the condition at which the recursive method will stop calling itself.
Base case
Worst Case
Best Case
None of the above
The following function finds the factorial of any number
int factorial(int n)
{
if(n == 0 || n == 1) return 1;
return n * factorial(n-1);
}
What would calling factorial(4) output?
24
16
8
64
The following function finds the factorial of any number
int factorial(int n)
{
if(n == 0 || n == 1) return 1;
return n * factorial(n-1);
}
What would calling factorial(2) output?
12
2
0
22
Make the following function return the following number in the fibonacci sequence
int fibo(int n)
{
if(n == 0 || n == 1) return n;
return _________________;
}
fib (n-1)+fib (n-2)
fibo(n-1)+fibo(n-2)
fibo(n*2)
fibo(n+1)+fibo(n+2)
True or False. Recursion can be used to fill in or print an array.
True
False
What is the following recursive function missing?
int fun1(int x, int y){
return fun1(x - 1, x + y);
}
A base case
A general case
a title
A return statement
What is the following recursive function missing?
int funTwo(int x, int y){
if (x == 0)
cout<< y;
else
cout<< fun1(x - 1, x + y);
}
A base case
A general case
the braces
A return statement
Complete the following function to be able to print an array recursively:
void print_array(int arr[], int size)
{
int i;
if (i == size) {
i = 0;
cout << endl;
return;
}
cout << ______<< " ";
i++;
print_array(arr, size);
}
arr[i]
i
size
print-array
Select all the examples of cases where recursive solutions are common
sorting
searching
displaying an image
setting variable values
