Recursion

Practice Questions

Computer Science › Recursion

Page 1 of 2
10 of 12
1

Which of the following is a recursive factorial function?

Recall that an example of a factorial is:

2

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

3

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)

4

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?

5

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

6

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?

7

Which of the following is a recursive method for summing the contents of a list of integers? (Choose the best answer.)

8

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?

9

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

10

The Fibonacci sequence is a classic example of: _________

Choose the best answer.

Page 1 of 2
Return to subject