Function: GATE CS Previous Year Questions

Function · Programming in C

68 questions · asked in 26 of 27 years (2000–2026)

MCQ 47 · NAT 211-mark 35 · 2-mark 33

2000Questions per year2026

Questions 1–5 of 68

Q1GATE 2026 (Session 1)1 markNAT
Consider the following program in C:
 #include <stdio.h>
 void func(int i, int j) {
  if(i < j) {
  	int i = 0;
  	while (i < 10) {
  		j += 2;
  		i++;
  	}
  }
  printf("%d", i);
 }
 int main() {
  	int i = 9, j = 10;
  	func(i, j);
  	return 0;
 }
The output of the program is ________. (answer in integer)
Note: Assume that the program compiles and runs successfully.

Q2GATE 2026 (Session 1)2 marksNAT
Consider the recursive functions represented by the following code segment:
int bar(int n) {
  if (n == 1) return 0;
  else return 1 + bar(n/2);
 }
 int foo(int n) {
  if (n == 1) return 1;
  else return 1 + foo(bar(n));
 } 
The smallest positive integer n for which foo(n)\text{foo(n)} returns 55 is ________. (answer in integer)
Note: Ignore syntax errors (if any) in the function.

Q3GATE 2026 (Session 2)2 marksNAT
Consider the following ANSI-C function.
 int func(int start, int end){
  	int length=end+1-start;
  	if((length<1)||(start<0)||(end<0)){ return(0); }
  	if(length%3==0){
  		return(func(start+1, end));
  	}else if(length%3==1){
  		return(1+func(start, end-1));
  	}else {
  		return(func(start+2, end));
  	}
 } 
The maximum possible value that can be returned from this function is ________. (answer in integer)
Note: Ignore syntax errors (if any) in the function.

Q4GATE 2025 (Session 1)2 marksNAT
#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 _________. (Answer in integer).

Q5GATE 2025 (Session 2)2 marksNAT
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 ________. (Answer in integer)

Keep practising

Topics in Programming in C

All CS topics ›