Functions GATE CS PYQ Quiz

Last Updated :
Discuss
Comments

Question 1

Consider the following ANSI C function:

int SomeFunction (int x, int y)
{
if ((x==1) || (y==1)) return 1;
if (x==y) return x;
if (x > y) return SomeFunction(x-y, y);
if (y > x) return SomeFunction (x, y-x);

}

The value returned by SomeFunction (15, 255) is __________ .

  • 15

  • 1275

  • 30

  • 255

Question 2

Consider the following ANSI C function:

int SimpleFunction(int Y[], int n, int x)
{
int total = Y[0], loopIndex;
for (loopIndex=1; loopIndex<=n-1; loopIndex++)
total=x*total +Y[loopIndex];
return total;
}

Let Z be an array of 10 elements with Z[i]=1, for all i such that 0 <= i <= 9. The value returned by SimpleFunction(Z,10,2) is __________ .

  • 1023

  • 1024

  • 2047

  • 511

Question 3

Consider the following C code. Assume that unsigned long int type length is 64 bits.

C
unsigned long int fun(unsigned long int n) {
        unsigned long int i, j = 0, sum = 0;
        for( i = n; i > 1; i = i/2) j++;
        for( ; j > 1; j = j/2) sum++;
        return sum;
}

The value returned when we call fun with the input [Tex]2^{40}[/Tex] is

  • 4

  • 5

  • 6

  • 40

Question 4

Consider the following C functions.

C
int tob (int b, int* arr) {
    int i;
    for (i = 0; b>0; i++)  {
        if (b%2)  arr [i] = 1;
        else      arr[i] = 0;
        b = b/2;
    }
    return (i);
}
 

int pp(int a, int b)  {
    int  arr[20];
    int i, tot = 1, ex, len;
    ex = a;
    len = tob(b, arr);
    for (i=0; i<len ; i++) {
         if (arr[i] ==1)
             tot = tot * ex;
         ex= ex*ex;
    }
return (tot) ;
}

The value returned by pp(3,4) is ________ .

Note -

This question was Numerical Type.

  • 81

  • 64

  • 100

  • 49

Question 5

Consider the following C-function:

C
double foo (int n)
{
    int i;
    double sum;
    if (n = = 0) return 1.0;
    else
    {
        sum = 0.0;
        for (i = 0; i < n; i++)
            sum += foo (i);
        return sum;
    }
}

Suppose we modify the above function foo() and store the values of foo (i), 0 < = i < n, as and when they are computed. With this modification, the time complexity for function foo() is significantly reduced. The space complexity of the modified function would be:

  • O(1)

  • O(n)

  • O(n!)

  • O(nn)

Question 6

Consider the following C-function: 

C
double foo (int n){
    int i;
    double sum;
    if (n = = 0) return 1.0;
    else{
        sum = 0.0;
        for (i = 0; i < n; i++)
            sum += foo (i);
        return sum;
    }
}

The space complexity of the above function is:

  • O(1)

  • O(n)

  • O(n!)

  • O(nn)

Question 7

Consider the following C program. 

C
#include<stdio.h>
void mystery(int *ptra, int *ptrb) 
{
   int *temp;
   temp = ptrb;
   ptrb = ptra;
   ptra = temp;
}
int main() 
{
    int a=2016, b=0, c=4, d=42;
    mystery(&a, &b);
    if (a < c)
       mystery(&c, &a);
    mystery(&a, &d);
    printf("%d", a);
}

The output of the program _____________   Note : This question was asked as Numerical Answer Type.

  • 2016

  • 0

  • 4

  • 8

Question 8

Consider the following snippet of a C program. Assume that swap(&x, &y) exchanges the contents of x and y.

int main()
{
int array[] = {3, 5, 1, 4, 6, 2};
int done = 0;
int i;

while (done == 0)
{
done = 1;
for (i = 0; i <= 4; i++)
{
if (array[i] < array[i+1])
{
swap(&array[i], &array[i+1]);
done = 0;
}
}
for (i = 5; i >= 1; i--)
{
if (array[i] > array[i-1])
{
swap(&array[i], &array[i-1]);
done = 0;
}
}
}

printf("%d", array[3]);
}

The output of the program is _____. Note: This question appeared as Numerical Answer Type.

  • 1

  • 2

  • 3

  • 4

Question 9

The value printed by the following program is
 

C
void f(int* p, int m)
{
    m = m + 5;
    *p = *p + m;
    return;
}
void main()
{
    int i=5, j=10;
    f(&i, j);
    printf("%d", i+j);
}
  • 10

  • 20

  • 30

  • 40

Question 10

Consider the following C-program:

C
void foo(int n, int sum)
{
  int k = 0, j = 0;
  if (n == 0) return;
    k = n % 10; 
  j = n / 10;
  sum = sum + k;
  foo (j, sum);
  printf ("%d,", k);
}
 
int main ()
{
  int a = 2048, sum = 0;
  foo (a, sum);
  printf ("%d\\n", sum);
   
  getchar();
}

What does the above program print?

  • 8, 4, 0, 2, 14

  • 8, 4, 0, 2, 0

  • 2, 0, 4, 8, 14

  • 2, 0, 4, 8, 0

There are 34 questions to complete.

Take a part in the ongoing discussion