Computer Science › Recursion
Which of the following is a recursive factorial function?
Recall that an example of a factorial is:
int lairotcaf (int n) {
if (n <= 1){
return 1
}
temp = n * lairotcaf(n-1)
return temp;
}
int num;
cin >> num;
``
if (num >= 0){
lairotcaf(n)
} else {
cout << "must use number greater or equal to zero"< endl;
}
What is returned by lairotcaf(5)
?
public static int foo(int a) {
if(a <= 3) {
return 1;
}
return a * foo(a - 3) * foo(a - 4);
}
What is the value of the following call to the function defined above:
foo(8)
public void draw() {
recurs(11);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What does the code print?
public static int foo(int a, int b) {
if(b <= 1 || b <= a) {
return 1;
}
return (b - a) * foo(a,b-1);
}
Based on the code above, what is the value of the following function call:
foo(5,9);
public void draw() {
recurs(10);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What is the output of the code?
Which of the following is a recursive method for summing the contents of a list of integers? (Choose the best answer.)
Consider the following code:
public static void main(String\[\] args) {
System.out.println(equation(8));
}
public static int equation(int a) {
if(a <= 5) {
return 12;
}
return equation(a-2) * equation(a-1);
}
What is the output for the code above?
public int foo(int n)
{
if (n < 0)
return 1;
else
return foo(n-2) + foo(n-1)
}
What is the value returned by foo(3)?
The Fibonacci sequence is a classic example of: _________
Choose the best answer.