C-Programming |Gate-2016| Previous Year Questions| Set-4

Set-15 C-Programming Gate 2005

C-Programming |Gate-2016|

1. Consider the following C program.

void f(int, short);
void main ()
{
     int i = 100;
     short s = 12;
     short *p = &s;
     __________ ;         // call to f()
}

Which one of the following expressions, when placed in the blank above, will NOT result in a type checking error? [GATE – 2016]

a. f(s, *s)
b. f(i, *s)
c. i = f(i, s)
d. f(i, *p)

Answer : d)


2. Consider the following C program.

#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\n”, a);
}

The output of the program is ________. [GATE – 2016]

a. 2016
b. 2017
c. 2018
d. 2019

Answer : a)
C-Programming |Gate-2016|


3. The following function computes the maximum value contained in an integer array p[] of size n (n >= 1). [GATE – 2016]

       int max(int *p, int n) {
           int a=0, b=n-1;
           while (__________) {
               if (p[a] <= p[b]) {a = a+1;}
               else                 {b = b-1;}
           }
           return p[a];
}

The missing loop condition is

a. a != n
b. b != 0
c. b > (a + 1)
d. b != a

Answer : d)


4. What will be the output of the following C program? [GATE – 2016]

void count(int n)  {
     static int d=1;
     printf(“%d “, n);
     printf(“%d “, d);
     d++;
     if(n>1) count(n-1);
     printf(“%d “, d);
}
 
void main() {
     count(3);
}

a. 3 1 2 2 1 3 4 4 4
b. 3 1 2 1 1 1 2 2 2
c. 3 1 2 2 1 3 4
d. 3 1 2 1 1 1 2

Answer : a)
C-Programming |Gate-2016|


5. What will be the output of the following pseudo-code when parameters are passed by reference and dynamic scoping is assumed? [GATE – 2016]

       a=3;
       void n(x) {x = x * a; print(x);}
       void m(y) {a = 1; a = y – a; n(a); print(a);}
void main() {m(a);}

a. 6, 2
b. 6, 6
c. 4, 2
d. 4, 4

Answer : d)


6. The value printed by the following program is __________. [GATE – 2016]

     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);
}

a. 30
b. 31
c. 32
d. 33

Answer : a)


7. The following function computes XY for positive integers X and Y.

       int exp (int X, int Y) {
           int res = 1, a = X, b = Y;
           while ( b != 0 ){
               if ( b%2 == 0) { a = a*a; b = b/2; }
               else           { res = res*a; b = b-1; }
           }
           return res;
}

Which one of the following conditions is TRUE before every iteration of the loop? [GATE – 2016]

a. XY = ab
b. (res * a)Y = (res * X)b
c. XY = res * ab
d. XY = (res * a)b

Answer  : c)


8. Consider the following program:

    int f(int *p, int n)
    {
       if (n <= 1) return 0;
       else return max (f(p+1,n-1),p[0]-p[1]);
    }
    int main()
    {
       int a[] = {3,5,2,6,4};
       printf(“%d”, f(a,5));
}

Note: max(x,y) returns the maximum of x and y.

The value printed by this program is __________. [GATE – 2016]

a. 3
b. 4
c. 5
d. 6

Answer : a)


OS – GATE Previous Year Questions
DS – GATE Previous Year Questions


Back to GATE-HOME


Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *