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

Last Updated :
Discuss
Comments

Question 1

Consider the following C program:

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


  • 10

Question 2

Match the following: 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


  • P→(ii), Q→(iv), R→(i), S→(iii)

  •  P→(ii), Q→(i), R→(iv), S→(iii)


  • P→(ii), Q→(iv), R→(iii), S→(i)

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



  • 0, 0

  • 0 ,1

  • 1, 0

  • 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)? 2017 SET 2|| MCQ || 2-mark


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


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


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

  •  (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]);
}

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




  • 3

Question 6

Consider the following C program.

C
#include<stdio.h>
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 CSE || 2017 SET 2 || NAT || 2-mark





  • 0

Question 7

Consider the following C program.

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




  • 2

Question 8

Consider the following C code:

C
#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: 2017 SET 1 ||MCQ || 1-mark


  •  compiler error as the return of malloc is not typecast approximately

  • compiler error because the comparison should be made as x==NULL and not as shown

  • compiles successfully but execution may result in dangling pointer

  • 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


  • 53423122233445

  •  53423120112233

  • 53423122132435

  • 53423120213243

Question 10

Consider the following C program.

C
#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 _________.   2017 SET 1 || NAT || 2-mark


  • 3

There are 58 questions to complete.

Take a part in the ongoing discussion