# Top MCQs on Recursion Algorithm with Answers

Question 1

Predict output of following program

C ```#include <stdio.h> int fun(int n) { if (n == 4) return n; else return 2*fun(n+1); } int main() { printf("%d", fun(2)); return 0; } ```
• 4

• 8

• 16

• Runtime Error

Question 2

Consider the following recursive function fun(x, y). What is the value of fun(4, 3)

C ```int fun(int x, int y) { if (x == 0) return y; return fun(x - 1, x + y); } ```
• 13

• 12

• 9

• 10

Question 3

What does the following function print for n = 25?

C ```void fun(int n) { if (n == 0) return; printf(\"%d\", n%2); fun(n/2); } ```
• 11001

• 10011

• 11111

• 00000

Question 4

What does the following function do?

C ```int fun(int x, int y) { if (y == 0) return 0; return (x + fun(x, y-1)); } ```
• x + y

• x + x*y

• x*y

• xy

Question 5

What does fun2() do in general?

C ```int fun(int x, int y) { if (y == 0) return 0; return (x + fun(x, y-1)); } int fun2(int a, int b) { if (b == 0) return 1; return fun(a, fun2(a, b-1)); } ```
• x*y

• x+x*y

• xy

• yx

Question 6

Output of following program?

C ```#include<stdio.h> void print(int n) { if (n > 4000) return; printf("%d ", n); print(2*n); printf("%d ", n); } int main() { print(1000); getchar(); return 0; } ```
• 1000 2000 4000

• 1000 2000 4000 4000 2000 1000

• 1000 2000 4000 2000 1000

• 1000 2000 2000 1000

Question 7

What does the following function do?

C ```int fun(unsigned int n) { if (n == 0 || n == 1) return n; if (n%3 != 0) return 0; return fun(n/3); } ```
• It returns 1 when n is a multiple of 3, otherwise returns 0

• It returns 1 when n is a power of 3, otherwise returns 0

• It returns 0 when n is a multiple of 3, otherwise returns 1

• It returns 0 when n is a power of 3, otherwise returns 1

Discuss it

Question 8

Predict the output of following program

C ```#include <stdio.h> int f(int n) { if(n <= 1) return 1; if(n%2 == 0) return f(n/2); return f(n/2) + f(n/2+1); } int main() { printf(\"%d\", f(11)); return 0; } ```
• Stack Overflow

• 3

• 4

• 5

Discuss it

Question 9

Predict the output:

C ```#include <stdio.h> void crazy(int n, int a, int b) { if (n <= 0) return; crazy(n - 1, a, b + n); printf("%d %d %d \n",n,a,b); crazy(n-1, b, a+n); } int main() { crazy(3, 4, 5); return 0; } ```
• ```1 4 10
2 4 8
1 8 6
3 4 5
1 5 9
2 5 7
1 7 7
```
• ```3 4 5
1 4 10
2 4 8
1 8 6
1 5 9
2 5 7
1 7 7
```
• ```1 4 10
2 4 8
1 8 6
3 4 5
```
• ```3 4 5
1 5 9
2 5 7
1 7 7
```
Discuss it

Question 10

Consider the following recursive C++ function that takes two arguments

C++ ``` unsigned int foo(unsigned int n, unsigned int r) { if (n > 0) return (n%r + foo (n/r, r )); else return 0; } ```

What is the return value of the function foo when it is called foo(345, 10)?

• 345

• 12

• 5

• 3

Discuss it

There are 30 questions to complete.

Last Updated :
Take a part in the ongoing discussion