Function: GATE CS Previous Year Questions

Function · Programming in C

68 questions · asked in 26 of 27 years (2000–2026)

MCQ 47 · NAT 211-mark 35 · 2-mark 33

2000Questions per year2026

Questions 41–45 of 68

Q41GATE 20091 markMCQ−0.33 if wrong
Consider the program below:
 #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;
} 
The value printed is

Q42GATE 20082 marksMCQ−0.67 if wrong
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);
    }
}

Q43GATE 20082 marksMCQ−0.67 if wrong
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);
  }
}

What does f(173) print?

Q44GATE 20082 marksMCQ−0.67 if wrong
Consider the following C functions:
int f1(int n)
{
  if(n == 0 || n == 1)
    return n;
  else
    return (2*f1(n-1) + 3*f1(n-2));
}
 
int f2(int n)
{
  int i;
  int X[N], Y[N], Z[N] ;
  X[0] = Y[0] = Z[0] = 0;
  X[1] = 1; Y[1] = 2; Z[1] = 3;
  for(i = 2; i <= n; i++)
  {
    X[i] = Y[i-1] + Z[i-2];
    Y[i] = 2*X[i];
    Z[i] = 3*X[i];
  }
  return X[n] ;
}
f1(8) and f2(8) return the values

Q45GATE 20082 marksMCQ−0.67 if wrong
Choose the correct option to fill ? 1 and ? 2 so that the program below prints an input string in reverse order. Assume that the input string is terminated by a newline character.
 void reverse(void) {
   int c;
   if (?1) reverse ();
   ?2
}
main() {
    printf("Enter Text "); printf("\n") ;
    reverse();  printf("\n");
}

Keep practising

Topics in Programming in C

All CS topics ›