C Programming GATE Questions

A highly popular method used to prepare for the GATE Exam is to practise all the previous years’ GATE Questions to perfection. Candidates can practise, analyse and understand concepts while solving them. It will also help you strengthen your time management skills. We have attempted to compile, here in this article, a collection of GATE Questions on C programming.

Candidates are urged to practise these C programming GATE previous year questions to get the best results. C programming is an important topic in the GATE CSE question paper, and solving these questions will help the candidates to prepare more proficiently for the GATE exams. Meanwhile, candidates can find the GATE Questions for C programming here, in this article below, to solve and practise before the exams. They can also refer to these GATE previous year question papers and start preparing for the exams.

GATE Questions on C Programming

  1. struct node
  2. {

    int i;

    float j;

    };

    struct node *s[10];

    The above C declaration defines

    GATE CSE 2000

        1. An array, each element of which is pointer to a structure of type node
        2. A structure of 2 fields, each field being a pointer to an array of 10 elements
        3. A structure of 3 fields: an integer, a float, and an array of 10 elements
        4. An array, each element of which is a structure of type node

    Answer (a)

  3. The number of tokens in
  4. printf(“i = %d, &i – %x”, i, &i);

    GATE CSE 2000

        1. 3
        2. 10
        3. 25
        4. 22

    Answer (b)

  5. Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment
  6. GATE CSE 2000

    struct {

    short s [5];

    union {

    float y;

    long z;

    }u;

    } t;

        1. 22 bytes
        2. 18 bytes
        3. 14 bytes
        4. 10 bytes

    Answer (b)

  7. Consider the given three C functions:
  8. [P1] int * g (void)

    {

    int x = 10;

    return (&x);

    }

    [P2] int * g (void)

    {

    int * px;

    *px = 10;

    return px;

    }

    [P3] int * g (void)

    {

    int * px

    px = (int *) malloc (sizeof(int));

    *px = 10;

    return px;

    }

    Which of the above three functions are likely to cause problems?

    GATE CSE 2001

        1. Only P1 and P2
        2. Only P3
        3. Only P1 and P3
        4. P1, P2, and P3

    Answer (a)

  9. What does the given program print?
  10. char c[ ] = “GATE2011”

    char *p = c;

    printf (“%s”, p + p[3] – p[1]);

    GATE CSE 2011

        1. GATE 2011
        2. 2011
        3. E2011
        4. 011

    Answer (b)

  11. The output of the following C program is__________
  12. GATE CSE 2015 Set 1

    void f1(int a, int b) {

    int c;

    c=a; a=b; b=c;

    }

    void f2(int *a, int *b) {

    int c;

    c=*a; *a=*b; *b=c;

    }

    int main(){

    int a=4, b=5, c=6;

    f1(a,b);

    f2(&b, &c);

    printf(“%d”,c-a-b);

    }

        1. -5
        2. 6
        3. -6
        4. 0

    Answer (a)

  13. The following program prints ___________
  14. #include < stdio.h >

    void f (int *p, int *q) {

    p = q;

    *p = 2;

    }

    int i = 0, j = 1;

    int main ( ){

    f(&i, &j);

    printf (“%d %d \ n”, i, j);

    return 0;

    }

    GATE CSE 2010

        1. 2 2
        2. 2 1
        3. 0 1
        4. 0 2

    Answer (d)

  15. Consider the following C program
  16. 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 CSE 2016 Set 1

        1. f(s,*s)
        2. i = f(i,s)
        3. f(i,*s)
        4. f(i,*p)

    Answer (d)

  17. The output of the following C program is
  18.                                                             GATE CSE 2018

    #include< stdio.h >

    struct Ournode{

    char x,y,z;

    };

    int main(){

    struct Ournode p = {‘1’, ‘0’, ‘a’+2};

    struct Ournode *q = &p;

    printf (“%c, %c”, *((char*)q+1), *((char*)q+2));

    return 0;

    }

        1. 0, c
        2. 0, a+2
        3. ‘0’, ‘a+2’
        4. ‘0’,’c’

    Answer (a)

  19. The output of the following C program is
  20. #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);

    }

    GATE CSE 2016 Set 1

        1. 2016
        2. 2018
        3. 016
        4. 16

    Answer (a)

  21. The output of the following C program is
  22. #include < stdio.h >

    int main () {

    int arr [] = {1,2,3,4,5,6,7,8,9,0,1,2,5}, *ip = arr+4;

    printf (“%d\n”, ip[1]);

    return 0;

    }

    GATE CSE 2019

        1. 6
        2. 5
        3. 66
        4. 0

    Answer (a)

  23. The most appropriate matching for the following pairs
  24. X: m=malloc(5); m= NULL;

    Y: free(n); n->value = 5;

    Z: char *p; *p=’a’;

    1: using dangling

    2: using uninitialized pointers

    3. lost memory

    is:

    GATE CSE 2000

        1. X-1 Y-3 Z-2
        2. X-2 Y-1 Z-3
        3. X-3 Y-2 Z-1
        4. X-3 Y-1 Z-2

    Answer (d)

  25. Consider the following C function
  26.                                                             GATE CSE 2004

    void swap (int a, int b)

    {

    int temp;

    temp = a;

    a = b;

    b = temp;

    }

    In order to exchange the values of two variables x and y

        1. call swap (x,y)
        2. call swap (&x, &y)
        3. swap (x, y) cannot be used as it does return any value
        4. swap (x,y) cannot be used as the parameters are passed by value

    Answer (d)

  27. Assume the following C variable declaration
  28. int * A[10], B[10][10];

    Of the following expressions

    I. A[2]

    II. A[2] [3]

    III. B[1]

    IV. B[2] [3]

    Which will not give compile-time errors if used as left-hand sides of assignment statements in a C program?

    GATE CSE 2003

        1. I, II and IV
        2. II, III and IV
        3. II and IV
        4. IV only

    Answer (a)

  29. In the C language
  30. a) At most one activation record exists between the current activation record and the activation record for the main

    b) The number of activation records between the current activation record and the activation record for the main depends on the actual function calling sequence.

    c) The visibility of global variables depends on the actual function calling sequence.

    d) Recursion requires the activation record for the recursive function to be saved on a different stack before the recursive function can be called.

    GATE CS 2002

        1. There is no such restriction in C language
        2. True
        3. False. In C, variables are statically scoped, not dynamically
        4. False. The activation records are stored on the same stack

    Answer (b)

  31. Consider the following C program:
  32. #include <stdio.h>

    int jumble(int x, int y){

    x=2*x+y;

    return x;

    }

    int main(){ 

    int x=2, y=5;

    y=jumble(y,x);

    x=jumble(y,x);

    printf(“%d \n”, x);

    return 0;

    }

    The value printed by the program is _____

    (GATE 2019)

        1. 26
        2. 25
        3. 20
        4. 0

    Answer (a)

  33. Consider the following C program:
  34.  

    #include <stdio.h>

     

    int main(){ 

     

    int arr[]={1,2,3,4,5,6,7,8,9,0,1,2,5}, *ip=arr+4;

     

    printf(“%d\n”, ip[1]);

     

    return 0;

     

    The number that will be displayed on execution of the program is _______

    (GATE 2019)

        1. 6
        2. 7
        3. 8
        4. 0

    Answer (a)

  35. Consider the following C function.
  36.  

    void convert(int n){ 

    if(n<0)

         printf(“%d”,n);

    else {

             convert(n/2);

             printf(“%d”,n%2);

    }

    }

     

    Which one of the following will happen when the function convert is called with any positive integer n as an argument?

    (GATE 2019)

        1. It will print the binary representation of n and terminate
        2. It will print the binary representation of n in the reverse order and terminate
        3. It will print the binary representation of n but will not terminate
        4. It will not print anything and will not terminate

    Answer (d)

  37. Consider the following C program:
  38. #include <stdio.h>

    int r(){

    static int num=7;

    return num–;

    }

    int main(){

    for (r();r();r())

    printf(“%d”,r());

    return 0;

    }

    Which one of the following values will be displayed on execution of the programs?

    (GATE 2019)

        1. 41
        2. 52
        3. 63
        4. 630

    Answer (b)

  39. Consider the following C program:
  40. #include <stdio.h> 

    int main(){

    float sum = 0.0, j = 1.0, i = 2.0; 

    while (i/j > 0.0625){

    j = j + j;

    sum = sum + i/j;

    printf(“%f\n”, sum);

    }

    return 0;

    }

    The number of times the variable sum will be printed, when the above program is executed, is _________

    (GATE 2019)

        1. 0
        2. 5
        3. 1
        4. None of the above

    Answer (b)

  41. Consider the following C program:
  42. #include <stdio.h>

    int main()

    {

    int a[ ] = {2, 4, 6, 8, 10};

    int i, sum = 0, *b = a + 4;

    for (i = 0; i < 5; i++)

    sum = sum + (*b – i) – *(b – i);

    printf (“%d\n”, sum);

    return 0;

    }

    The output of the above C program is ______

    (GATE 2019)

        1. 10
        2. 12
        3. 15
        4. 20

    Answer (a)

  43. Consider the following C program:
  44.  

    #include<stdio.h>

     

    void fun1(char *s1, char *s2){

         char *tmp;

         tmp = s1;

         s1 = s2;

         s2 = tmp;

    }

    void fun2(char **s1, char **s2){

         char *tmp;

         tmp = *s1;

         *s1 = *s2;

         *s2 = tmp;

    }

    int main(){

        char *str1 = “Hi”, *str2 = “Bye”;

        fun1(str1, str2); printf(“%s %s “, str1, str2);

        fun2(&str1, &str2); printf(“%s %s”, str1, str2);

        return 0;

    }

    The output of the program above is

    (GATE 2018)

        1. Hi Bye Bye Hi
        2. Hi Bye Hi Bye
        3. Bye Hi Hi Bye
        4. Bye Hi Bye Hi

    Answer (a)

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

    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 240 is

    (GATE 2018)

        1. 4
        2. 5
        3. 6
        4. 40

    Answer (a)

  47. Consider the following C program
  48. #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)

        1. 3
        2. 5
        3. 7
        4. 9

    Answer (a)

  49. The output of executing the following C program is ____________ .
  50.  

    #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 (; i>0;i–) {

            x=x+total(i);

        }

        printf(“%d\n”,x);

    }

    (GATE 2017)

        1. 23
        2. 25
        3. 27
        4. 29

    Answer (a)

  51. Consider the following function implemented in C:
  52. 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)

        1. 0, 0
        2. 0, 1
        3. 1, 0
        4. 1, 1

    Answer (c)

  53. Consider the C program fragment below which is meant to divide x by y using repeated subtraction. The variables x, y, q and r are all unsigned int.
  54. while (r >=y) {

      r = r – y;

      q = q + 1;

    }

    Which of the  following condition 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)

        1. (q == r)  && (r == 0)
        2. (x > 0) && (r == x) && (y > 0)
        3. (q == 0) && (r == x) && (y > 0)
        4. (q == 0) && (y > 0)

    Answer (c)

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

    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 ______

    (GATE 2017)

        1. 3
        2. 4
        3. 5
        4. 6

    Answer (a)

  57. Consider the following C program.
  58.  

    #include<stdio.h>

    int main () {

              int m = 10;

              int n, nl;

              n = ++m;

              n1 = m++;

              n–;

              –n1;

              n-= n1;

              printf (“%d”,n),

              return 0;

    }

    The output of the program is _________

    (GATE 2017)

        1. 0
        2. 1
        3. 5
        4. None of the above

    Answer (a)

  59. What will be the output of the following C program?
  60.  

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

       }

    (GATE 2016)

        1. 3 1 2 2 1 3 4 4 4
        2. 3 1 2 1 1 1 2 2 2
        3. 3 1 2 2 1 3 4 
        4. 3 1 2 1 1 1 2

    Answer (a)

Keep learning and stay tuned to get the latest updates on the GATE Syllabus for CSE (Computer Science Engineering), Introduction to C Programming, GATE CSE Question Paper, and more.

Leave a Comment

Your Mobile number and Email id will not be published. Required fields are marked *

*

*