C Programming GATE CS and IT previous year questions with Answer


Ques 11 Gate 2019


Consider the following C program:

#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 ", x);
    return 0;
}


The value printed by program is __________


a is the correct answer.


Ques 12 Gate 2019


Consider the following C program:

#include<stdio.h>
int r(){
    int static 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?

A

41

B

52

C

63

D

630



Ques 13 Gate 2019


Consider the following C program:

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 argument?

A

It will print the binary representation of n in the reverse order and terminate.

B

It will print the binary representation of n but will not terminate.

C

It will not print anything and will not terminate.

D

It will print the binary representation of n and terminate.



Ques 14 Gate 2018


Consider the following program written in pseudo-code. Assume that x and y are integers.

Count (x, y) {
    if (y !=1 ) {
        if (x !=1) {
            print("*");
            Count (x/2, y);
      }
    else {
      y=y-1;
      Count (1024, y);
     }
   }
}

The number of times that the print statement is executed by the call Count(1024, 1024) is _______ .


a is the correct answer.


Ques 15 Gate 2018


Consider the following C program:

#include<stdio.h>
int counter = 0;
int calc(int a, int b) {
    int c;
    counter++;
    if (b == 3)
       return (a * a * a);
   else {
       c = calc(a, b / 3);
        return (c * c * c);
    }
}
int main() {
   calc(4, 81);
   printf("%d", counter);
}


The output of this program is ________ .


a is the correct answer.


Ques 16 Gate 2018


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

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

A

4

B

5

C

6

D

40



Ques 17 Gate 2018


Consider the following C program.

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

The output of this program is:

A

0, c

B

0, a+2

C

'0', 'a+2'

D

'0', 'c'



Ques 18 Gate 2017 Set-2


Consider the C program fragment below which is meant to divide x by y using repeated subtractions. The variable 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)?

A

( q == r ) && ( r == 0)

B

( x <0 ) && ( r == x ) && ( y > 0 )

C

( q == 0 ) && ( r == x ) && ( y > 0 )

D

( q == 0 ) && ( y > 0 )



Ques 19 Gate 2017 Set-2


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 the printxy(1,1) is

A

0,0

B

0,1

C

1,0

D

1,1



Ques 20 Gate 2017 Set-1


The output of executing the following C program is ________.

# 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 (“%dn”, x) ;
}


a is the correct answer.