C-Programming |Gate-2017| Previous Year Questions| Set-3

Set-15 C-Programming Gate 2005

C-Programming |Gate-2017|

1. Consider the following C code:

             #include <stdio.h>
             int *assignval(int *x, int val)   {
                  *x = val;
                  return x;
             }
             void main ( )  {
                  int  *x = malloc(sizeof(int));
                  if(NULL == x)  return;
                     x = assignval(x, 0);
                     if(x)  {
                           x = (int *)malloc(size of(int));
                           if(NULL == x)  return;
                              x = assignval(x, 10);
                     }
                     printf(“%dn”,  *x);
                     free(x);
}

The code suffers from which one of the following problems: [GATE -2017]

a. compiler error as the return of malloc is not typecast approximately
b. compiler error because the comparison should be made as x==NULL and not as shown
c. compiles successfully but execution may result in dangling pointer
d. compiles successfully but execution may result in memory leak

Answer : d)


2. Consider the following two functions.

void fun1(int n)   {                 void fun2(int n)   {
        if(n == 0)  return;                  if(n == 0)  return;
        printf(“%d”, n);                     printf(“%d”, n);
        fun2(n – 2);                         fun1(++n);
        printf(“%d”, n);                     printf(“%d”, n);
}     
}

The output printed when fun1(5) is called is : [GATE -2017]

a. 53423122233445
b. 53423120112233
c. 53423122132435
d. 53423120213243

Answer : a)


3. Consider the C functions foo and bar given below:

         int foo(int val)   {
                 int x = 0;
                 while (val>0)   {
                    x = x + foo(val–);
                 }
                 return val;
               }
 
               int bar(int val)   {
                  int x = 0;
                  while (val>0)   {
                      x = x + bar(val-1);
                  }
                  return val;
}

Invocations of foo(3) and bar(3) will result in: [GATE -2017]

a. Return of 6 and 6 respectively.
b. Infinite loop and abnormal termination respectively
c. Abnormal termination and infinite loop respectively.
d. Both terminating abnormally.

Answer : c)


4. Consider the following C program.

#include <stdio.h>
#include <string.h>
void printlength (char*s, char*t)   {
   unsigned int c = 0;
   int len = ((strlen(s) – strlen(t)) > c) ? strlen(s):strlen(t);
   printf(“%d\n”, len);
}
void main ()  {
   char*x = “abc”;
   char*y = “defgh”;
   printlength(x,y);
}

Recall that strlen is defined in string.h as returning a value of type size_t, which is an unsigned int. The output of the program is _________.   [GATE -2017]

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

Answer : a)


5. The output of executing the following C program is __________. [GATE -2017]

           #include<stdio.h>
           int total (int v)   {
               static int count=0;
               while(v)   {
                   count += v&1;
                   v ≫= 1;
               }    
               return count;
           }
           void main()    {
               static int x = 0;
               int i = 5;
               for(; 1> 0; i–)    {
                  x = x + total(i);
               }
               printf(“%d\n”, x);
}

a. 23
b. 24
c. 25
d. 26

Answer : a)


6. Match the following: [GATE -2017]

a. P→(ii), Q→(iv), R→(i), S→(iii)
b. P→(ii), Q→(i), R→(iv), S→(iii)
c. P→(ii), Q→(iv), R→(iii), S→(i)
d. P→(iii), Q→(iv), R→(i), S→(ii)

Answer : a)


7. Consider the following function implemented in C:

void printxy (int x, int y)   {
      int *ptr;
      x = 0;
      ptr = &x;
      y = *ptr;
      *ptr = 1;
      printf(“%d,%d”,x,y);
}

The output of invoking printxy(1, 1) is : [GATE -2017]

a. 0,0
b. 0,1
c. 1,0
d. 1,1

Answer : c)


8. Consider the C program fragment below which is meant to divide x and y using repeated subtractions. The variables x, y, q and r are all unsigned int.

while (r >= y)  {
r = r – y;
q = q + 1;
}

Which of the following conditions on the variables x, y, q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x == (y*q + r)? [GATE -2017]

a. (q == r) && (r == 0)
b. (x > 0) && (r == x) && (y > 0)
c. (q == 0) && (r == x) && (y > 0)
d. (q == 0) && (y > 0)

Answer : c)
C-Programming |Gate-2017|


9. 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, i;
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 ___________. : [GATE -2017]

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

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


10. Consider the following C program.

#include
int main ()
{
int m=10;
int n, n1;
n=++m;
n1=m++;
n–;
–n1;
n-=n1;
printf(“%d”, n);
return 0;
}

The output of the program is ______ : [GATE -2017]

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

Answer : a)


11. Consider the following C program.

#include<stdio.h>
#include<string.h>
int main ()  {
char* c = “GATECSIT2017”;
char* p = c;
printf(“%d”, (int) strlen (c + 2[p] – 6[p] – 1));
return 0;
}

The output of the program is __________. [GATE -2017]

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

Answer : b)


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 *