C · previous year questions

All PYQs: C Programming

47 real GATE CSE questions from 2012 to 2025, with the official answer key and a worked solution for each. Filter by year, topic, type, marks or keyword.

Questions belong to the GATE organising institutes; this copy is for personal study. Coverage varies by year: questions that need a figure I could not read, or whose answer I could not verify, were left out.

Showing 47 of 47 questions · 0 correct of 0 attempted · marks 0 / 75

GATE 2025 CS2 · Q19MCQ1 markStrings & Characters
Consider the following C program:
#include <stdio.h>
void stringcopy(char *, char *);
int main(){
  char a[30] = "@#Hello World!";
  stringcopy(a, a + 2);
  printf("%s\n", a);
  return 0;
}
void stringcopy(char *s, char *t) {
  while(*t)
    *s++ = *t++;
}
Which ONE of the following will be the output of the program?
GATE 2025 CS2 · Q33NAT1 markControl Flow & Output
Consider the following C code segment.
int x=126,y=105;
do {
  if(x>y) x=x-y;
  else y=y-x;
} while(x!=y);
printf("%d",x);
The output of the given C code segment is ______.
GATE 2025 CS1 · Q34NAT1 markFunctions & Recursion
Consider the following C program.
#include <stdio.h>
void foo(int *p, int x){
  *p=x;
}
int main(){
  int *z;
  int a = 20, b = 25;
  z = &a;
  foo(z,b);
  printf("%d",a);
  return 0;
}
The output of the given C program is ______.
GATE 2025 CS1 · Q61NAT2 marksFunctions & Recursion
Consider the following C program.
#include <stdio.h>
int foo(int S[],int size){
  if(size == 0) return 0;
  if(size == 1) return 1;
  if(S[0] != S[1]) return 1+foo(S+1,size-1);
  return foo(S+1,size-1);
}
int main(){
  int A[]={0,1,2,2,2,0,0,1,1};
  printf("%d",foo(A,9));
  return 0;
}
The value printed by the given C program is ______.
GATE 2025 CS2 · Q62NAT2 marksPointers & Arrays
Consider the following C program:
#include <stdio.h>
int main(){
  int a;
  int arr[5] = {30,50,10};
  int *ptr;
  ptr = &arr[0] + 1;
  a = *ptr;
  (*ptr)++;
  ptr++;
  printf("%d", a + (*ptr) + arr[1]);
  return 0;
}
The output of the above program is ______.
GATE 2025 CS1 · Q63NAT2 marksControl Flow & Output
Consider the following C program:
#include <stdio.h>
int gate (int n) {
  int d, t, newnum, turn;
  newnum = turn = 0; t=1;
  while (n>=t) t *= 10;
  t /=10;
  while (t>0) {
    d = n/t;
    n = n%t;
    t /= 10;
    if (turn) newnum = 10*newnum + d;
    turn = (turn + 1) % 2;
  }
  return newnum;
}
int main () {
  printf ("%d", gate(14362));
  return 0;
}
The value printed by the given C program is ______.
GATE 2025 CS2 · Q63NAT2 marksFunctions & Recursion
Consider the following C program:
#include <stdio.h>
int g(int n) {
  return (n+10);
}
int f(int n) {
  return g(n*2);
}
int main() {
  int sum, n;
  sum=0;
  for (n=1; n<3; n++)
    sum += g(f(n));
  printf ("%d", sum);
  return 0;
}
The output of the given C program is ______.
GATE 2024 CS2 · Q13MCQ1 markFunctions & Recursion
Consider the following C program. Assume parameters to a function are evaluated from right to left.
#include <stdio.h>

int g(int p) { printf("%d", p); return p; }
int h(int q) { printf("%d", q); return q; }

void f(int x, int y) {
  g(x);
  h(y);
}

int main() {
  f(g(10),h(20));
}
Which one of the following options is the CORRECT output of the above C program?
GATE 2024 CS1 · Q18MCQ1 markControl Flow & Output
Consider the following C program:
#include <stdio.h>
int main(){
  int a = 6;
  int b = 0;
  while(a < 10) {
    a = a / 12 + 1;
    a += b;
  }
  printf("%d", a);
  return 0;
}
Which one of the following statements is CORRECT?
GATE 2024 CS1 · Q19MCQ1 markFunctions & Recursion
Consider the following C program:
#include <stdio.h>
void fX();
int main(){
  fX();
  return 0;}

void fX(){
  char a;
  if((a=getchar()) != '\n')
    fX();
  if(a != '\n')
    putchar(a);}
Assume that the input to the program from the command line is 1234 followed by a newline character. Which one of the following statements is CORRECT?
GATE 2024 CS2 · Q33MSQ1 markStrings & Characters
Consider the following C function definition.
int fX(char *a){
  char *b = a;
  while(*b)
    b++;
  return b - a;
}
Which of the following statements is/are TRUE?

One or more options may be correct. Select all that apply, then submit.

GATE 2024 CS2 · Q36MCQ2 marksPointers & Arrays
What is the output of the following C program?
#include <stdio.h>
int main() {
  double a[2]={20.0, 25.0}, *p, *q;
  p = a;
  q = p + 1;
  printf("%d,%d", (int)(q - p), (int)(*q - *p));
  return 0;
}
GATE 2024 CS1 · Q48MSQ2 marksTypes, Operators & Expressions
Consider the following C function definition.
int f(int x, int y) {
  for (int i=0; i<y; i++) {
    x=x+x+y;
  }
  return x;
}
Which of the following statements is/are TRUE about the above function?

One or more options may be correct. Select all that apply, then submit.

GATE 2023 · Q35NAT1 markStorage Classes, Scope & Memory
The integer value printed by the ANSI-C program given below is ______.
#include<stdio.h>
int funcp(){
  static int x = 1;
  x++;
  return x;
}
int main(){
  int x,y;
  x = funcp();
  y = funcp()+x;
  printf("%d\n", (x+y));
  return 0;
}
GATE 2023 · Q57NAT2 marksPointers & Arrays
Consider the following two-dimensional array D in the C programming language, which is stored in row-major order:
int D[128][128];
Demand paging is used for allocating memory and each physical page frame holds 512 elements of the array D. The Least Recently Used (LRU) page-replacement policy is used by the operating system. A total of 30 physical page frames are allocated to a process which executes the following code snippet:
for (int i = 0; i < 128; i++)
  for (int j = 0; j < 128; j++)
    D[j][i] *= 10;
The number of page faults generated during the execution of this code snippet is ______.
GATE 2022 · Q21MCQ1 markPointers & Arrays
What is printed by the following ANSI C program?
#include<stdio.h>
int main(int argc, char *argv[])
{
  int x = 1, z[2] = {10, 11};
  int *p = NULL;
  p = &x;
  *p = 10;
  p = &z[1];
  *(&z[0] + 1) += 3;
  printf("%d, %d, %d\n", x, z[0], z[1]);
  return 0;
}
GATE 2022 · Q43MCQ2 marksPointers & Arrays
What is printed by the following ANSI C program?
#include<stdio.h>
int main(int argc, char *argv[])
{
  int a[3][3][3] =
    {{1, 2, 3, 4, 5, 6, 7, 8, 9},
     {10, 11, 12, 13, 14, 15, 16, 17, 18},
     {19, 20, 21, 22, 23, 24, 25, 26, 27}};
  int i = 0, j = 0, k = 0;
  for( i = 0; i < 3; i++ ){
    for(k = 0; k < 3; k++ )
      printf("%d ", a[i][j][k]);
    printf("\n");
  }
  return 0;
}
GATE 2022 · Q44MCQ2 marksTypes, Operators & Expressions
What is printed by the following ANSI C program? ASCII: P = 80, x = 120, * = 42, - = 45, + = 43, z = 122, K = 75, S = 83.
#include<stdio.h>
int main(int argc, char *argv[]){
  char a = 'P';
  char b = 'x';
  char c = (a & b) + '*';
  char d = (a | b) - '-';
  char e = (a ^ b) + '+';
  printf("%c %c %c\n", c, d, e);
  return 0;
}
GATE 2021 Set 1 · Q37MCQ2 marksControl Flow & Output
Consider the following ANSI C program.
#include <stdio.h>
int main()
{
  int i, j, count;
  count = 0;
  i = 0;
  for (j = -3; j <= 3; j++)
  {
    if ((j >= 0) && (i++))
      count = count + j;
  }
  count = count + i;
  printf("%d", count);
  return 0;
}
Which one of the following options is correct?
GATE 2021 Set 1 · Q48NAT2 marksControl Flow & Output
Consider the following ANSI C function:
int SimpleFunction(int Y[], int n, int x)
{
  int total = Y[0], loopIndex;
  for (loopIndex = 1; loopIndex <= n - 1; loopIndex++)
    total = x * total + Y[loopIndex];
  return total;
}
Let Z be an array of 10 elements with Z[i] = 1, for all i such that 0 ≤ i ≤ 9. The value returned by SimpleFunction(Z, 10, 2) is ______.
GATE 2020 · Q22NAT1 markPointers & Arrays
Consider the following C program.
#include <stdio.h>
int main() {
  int a[4][5] = {{1, 2, 3, 4, 5},
                 {6, 7, 8, 9, 10},
                 {11, 12, 13, 14, 15},
                 {16, 17, 18, 19, 20}};
  printf("%d\n", *(*(a+**a+2)+3));
  return(0);
}
The output of the program is ______.
GATE 2020 · Q46NAT2 marksStorage Classes, Scope & Memory
Consider the following C functions.
int fun1(int n) {            int fun2(int n) {
  static int i = 0;             static int i = 0;
  if (n > 0) {                  if (n > 0) {
    ++i;                          i = i + fun1(n);
    fun1(n-1);                    fun2(n-1);
  }                             }
  return(i);                    return(i);
}                             }
The return value of fun2(5) is ______.
GATE 2020 · Q48NAT2 marksFunctions & Recursion
Consider the following C functions.
int tob(int b, int* arr) {
  int i;
  for(i=0; b>0; i++) {
    if(b%2) arr[i]=1;
    else arr[i]=0;
    b = b/2;
  }
  return(i);
}
int pp(int a,int b) {
  int arr[20];
  int i, tot = 1, ex, len;
  ex = a;
  len = tob(b,arr);
  for(i=0; i<len; i++) {
    if(arr[i]==1)
      tot = tot * ex;
    ex = ex * ex;
  }
  return(tot);
}
The value returned by pp(3,4) is ______.
GATE 2019 Set 2 · Q18NAT1 markFunctions & Recursion
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 \n", x);
  return 0;
}
The value printed by the program is ______.
GATE 2019 Set 2 · Q24NAT1 markPointers & Arrays
Consider the following C program:
#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 Set 2 · Q26MCQ2 marksFunctions & Recursion
Consider the following C function.
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?
GATE 2019 Set 2 · Q27MCQ2 marksStorage Classes, Scope & Memory
Consider the following C program:
#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 Set 2 · Q52NAT2 marksControl Flow & Output
Consider the following C program:
#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 Set 2 · Q53NAT2 marksPointers & Arrays
Consider the following C program:
#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 2018 · Q2MCQ1 markStructs, Unions & Undefined Behaviour
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:
GATE 2018 · Q21NAT1 markFunctions & Recursion
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 ______.
GATE 2018 · Q29MCQ2 marksPointers & Arrays
Consider the following C program:
#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 · Q32MCQ2 marksControl Flow & Output
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
GATE 2017 Set 1 · Q11MCQ1 markStructs, Unions & Undefined Behaviour
Consider the C struct defined below:
struct data {
  int marks [100];
  char grade;
  int cnumber;
};
struct data student;
The base address of student is available in register R1. The field student.grade can be accessed efficiently using
GATE 2017 Set 1 · Q13MCQ1 markStorage Classes, Scope & Memory
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(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 · Q35MCQ2 marksFunctions & Recursion
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 Set 1 · Q53NAT2 marksStrings & Characters
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 Set 1 · Q55NAT2 marksStorage Classes, Scope & Memory
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("%d\n", x);
}
GATE 2016 Set 1 · Q12MCQ1 markFunctions & Recursion
Consider the following C program.
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 2016 Set 1 · Q15NAT1 markPointers & Arrays
Consider the following C program.
#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);
}
The output of the program is ______.
GATE 2016 Set 1 · Q35MCQ2 marksStorage Classes, Scope & Memory
What will be the output of the following C program?
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 2014 Set 1 · Q10MCQ1 markPointers & Arrays
Consider the following program in C language:
#include <stdio.h>
main()
{
  int i;
  int *pi = &i;
  scanf("%d",pi);
  printf("%d\n", i+5);
}
Which one of the following statements is TRUE?
GATE 2014 Set 1 · Q41MCQ2 marksControl Flow & Output
Consider the following C function in which size is the number of elements in the array E:
int MyX(int *E, unsigned int size)
{
  int Y = 0;
  int Z;
  int i, j, k;
  for(i = 0; i < size; i++)
    Y = Y + E[i];
  for(i = 0; i < size; i++)
    for(j = i; j < size; j++)
    {
      Z = 0;
      for(k = i; k <= j; k++)
        Z = Z + E[k];
      if (Z > Y)
        Y = Z;
    }
  return Y;
}
The value returned by the function MyX is the
GATE 2014 Set 1 · Q42MCQ2 marksControl Flow & Output
Consider the following pseudo code. What is the total number of multiplications to be performed?
D = 2
for i = 1 to n do
  for j = i to n do
    for k = j + 1 to n do
      D = D * 3
GATE 2013 Code A · Q31MCQ2 marksControl Flow & Output
Consider the following function:
int unknown(int n){
  int i, j, k=0;
  for (i=n/2; i<=n; i++)
    for (j=2; j<=n; j=j*2)
      k = k + n/2;
  return (k);
}
The return value of the function is
GATE 2012 Code A · Q48MCQ2 marksStorage Classes, Scope & Memory
Common data for Questions 48 and 49. Consider the following C code segment.
int a, b, c = 0;
void prtFun(void);
main( )
{
  static int a = 1;      /* Line 1 */
  prtFun( );
  a += 1;
  prtFun( );
  printf(" \n %d  %d ", a, b);
}

void prtFun(void)
{
  static int a = 2;      /* Line 2 */
  int b = 1;
  a += ++b;
  printf(" \n %d  %d ", a, b);
}
What output will be generated by the given code segment? (Each option lists three lines.)
GATE 2012 Code A · Q49MCQ2 marksStorage Classes, Scope & Memory
With the same code as Question 48, what output will be generated if Line 1 is replaced by auto int a = 1; and Line 2 is replaced by register int a = 2;?