Question 11
Consider the same 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 as foo(513, 2)?
Question 12
Predict the Output:
class GFG
{
static int f(int a[],int i, int n)
{
if(n <= 0) return 0;
else if(a[i] % 2 == 0) return a[i] + f(a, i+1, n-1);
else return a[i] - f(a, i+1, n-1);
}
public static void main(String args[])
{
int a[] = {12, 7, 13, 4, 11, 6};
System.out.print(f(a,0,6));
}
}
Question 13
Output of following program?
#include <stdio.h>
int fun(int n, int *f_p)
{
int t, f;
if (n <= 1)
{
*f_p = 1;
return 1;
}
t = fun(n- 1,f_p);
f = t+ * f_p;
*f_p = t;
return f;
}
int main()
{
int x = 15;
printf (\" %d \\n\", fun(5, &x));
return 0;
}
Question 14
Consider the JAVA function given below.
static int f(int j){
int i = 50;
int k;
if (i == j){
System.out.print("something");
k = f(i);
return 0;
}
else return 0;
}
Which one of the following is TRUE?
Question 15
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
Question 16
Consider the following C function.
int fun (int n){
int x = 1, k;
if (n == 1)
return x;
for (k = 1; k < n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}
The return value of fun(5) is __________.
Question 17
Consider the following recursive JAVA function. If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main()?
static void get (int n){
if (n < 1)
return;
get(n - 1);
get(n - 3);
System.out.print(n);
}
Question 18
What will be the output of the following JAVA program?
class GFG
{
static int d=1;
static void count(int n)
{
System.out.print(n+" ");
System.out.print(d+" ");
d++;
if(n > 1) count(n-1);
System.out.print(d+" ");
}
public static void main(String args[])
{
count(3);
}
}
Question 19
Consider the code fragment written in C below :
void f (int n)
{
if (n <= 1) {
printf (\"%d\", n);
}
else {
f (n/2);
printf (\"%d\", n%2);
}
}
Which of the following implementations will produce the same output for f(173) as the above code? P1
void f (int n)
{
if (n/2) {
f(n/2);
}
printf (\"%d\", n%2);
}
P2
void f (int n)
{
if (n <=1) {
printf (\"%d\", n);
}
else {
printf (\"%d\", n%2);
f (n/2);
}
}
Question 20
In general, in a recursive and non-recursive implementation of a problem (program) :
There are 30 questions to complete.