GATE || C Programming || PYQS (2010 to 2025)

Last Updated :
Discuss
Comments

Question 1

Consider the following C program:

C
#include 
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 CSE || 2019 NAT || 2-mark]

Question 2

Match the following: [GATE-2017 || SET 2 MCQ || 1-mark ]

(P) static char var ;

(i) Sequence of memory locations to store address

(Q) m = malloc (10); 

   m = NULL;

(ii) A variable located in data section of memory

(R) char*ptr[10];

(iii) Request to allocate a CPU register to store data

(S) register int var1;

(iv) A lost memory which cannot be freed

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

Question 3

Consider the following function implemented in C:

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 || SET 2 MCQ || 2-mark]

  • A

    0, 0

  • B

    0 ,1

  • C

    1, 0

  • D

    1, 1

Question 4

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

         

C
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 || SET 2 MCQ || 2-mark]

  • A

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

  • B

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

  • C

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

  • D

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

Question 5

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

C
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]);
    return 0;
}

The output of the program is ________. [GATE-2017 || SET 2 NAT || 2-mark ]

Question 6

Consider the following C program.

C
#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 || SET 2 NAT || 2-mark]

Question 7

Consider the following C program.

C
#include 
#include 

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 || SET 2 NAT || 2-mark ]

Question 8

Consider the following C code:

C
#include 
#include 

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(sizeof(int));
        if (NULL == x) 
            return;
        x = assignval(x, 10);
    }
    
    printf("%d\n", *x);
    free(x);
}

The code suffers from which one of the following problems: [GATE-2017 || SET 1 MCQ || 1-mark]

  • 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

Question 9

Consider the following two functions.

C
void fun1(int n) {
    if(n == 0) return;
    printf("%d", n);
    fun2(n - 2);
    printf("%d", n);
}
void fun2(int n) {
    if(n == 0) return;
    printf("%d", n);
    fun1(++n);
    printf("%d", n);
}


The output printed when fun1(5) is called is 2017 SET 1 || MCQ || 2-mark

  • A

    53423122233445

  • B

     53423120112233

  • C

    53423122132435

  • D

    53423120213243

Question 10

Consider the following C program.

C
#include 
#include 

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 || SET 1 || NAT || 2-mark]

There are 58 questions to complete.

Take a part in the ongoing discussion