1.
#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;
}
(a) 4
(b) 8
(c) 16
(d) Runtime Error
Answer
Fun(2)
|
2*fun(3)
|
2*fun(4)
|
4
After tree evaluation we get 16
So, C is the correct answer
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);
}
(a) 13
(b) 12
(c) 9
(d) 10
Answer
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);
}
(a) 11001
(b) 10011
(c) 11111
(d) 00000
4.
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));
}
(a) x*y
(b) x+x*y
(c) xy
(d) yx
5.
#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;
}
(a) Stack Overflow
(b) 3
(c) 4
(d) 5
6.
Consider the following C function:
int f(int n)
{
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is
(a) 5
(b) 6
(c) 7
(d) 8
Execution of f(1)
i = 1
n = 2
i = 2
Call f(2)
i = 2
n = 4
i = 3
Call f(4)
i = 3
n = 7
i = 4
Call f(7)
since n >= 5 return n(7)
7.
# include “stdio.h”
void abc()
{
int a=10;
printf(“\n hello abc : %d”, ++a);
return a;
}
void main()
{
int x;
x=abc();
printf(“\n x=%d”, x);
}
(a) 11
(b) 10
(c) Compilation error
(d) None .
Answer
8.
# include “stdio.h”
int main()
{
int a=8.5,b;
a= func1(a=func1(a=func1(a)));
printf(“a=%d”,a);
}
int func1(int a)
{
a–;
return(a);
}
(a) 7
(b) 5
(c) 6
(d) Error
9.
# include “stdio.h”
int sum( int x)
{
int s;
s=s+sum(x-1);
return(s);
}
int main()
{
int y;
y=sum(5);
printf(“%d”,y);
}
(a) 15
(b) 0
(c) Runtime error
(d) None
Answer
10.
void f (int n)
{
if (n <=1) {
printf (“%d”, n);
}
else {
f (n/2);
printf (“%d”, n%2);
}
}
main()
{
f(173);
}
(a) 010110101
(b) 010101101
(c) 10110101
(d) 10101101