Question 1
Predict output of following program
#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;
}
Question 2
Consider the following recursive function fun(x, y). What is the value of fun(4, 3)
int fun(int x, int y)
{
if (x == 0)
return y;
return fun(x - 1, x + y);
}
Question 3
What does the following function print for n = 25?
void fun(int n)
{
if (n == 0)
return;
printf(\"%d\", n%2);
fun(n/2);
}
Question 4
What does the following function do?
int fun(int x, int y)
{
if (y == 0) return 0;
return (x + fun(x, y-1));
}
Question 5
What does fun2() do in general?
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));
}
Question 6
Output of following program?
#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;
}
Question 7
What does the following function do?
int fun(unsigned int n)
{
if (n == 0 || n == 1)
return n;
if (n%3 != 0)
return 0;
return fun(n/3);
}
Question 8
Predict the output of following program
#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;
}
Question 9
Predict the output:
#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;
}
Question 10
Consider the following recursive C++ function that takes two arguments
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)?
There are 30 questions to complete.