Last updated on February 9th, 2025 at 09:26 am
Here, We will see C Programming GATE Questions from previous year’s papers and the syllabus of C Programming for GATE Exam.
Table of Contents
1. C Programming in Gate CSE Exam
C Programming is a cornerstone of the GATE Computer Science and Information Technology (CS/IT) exam, testing candidates’ understanding of fundamental concepts like pointers, memory management, data structures, and function behavior. These questions evaluate problem-solving skills and technical proficiency, making them critical for securing high scores.
2. C Programming Syllabus
The syllabus for C Programming in GATE CSE includes:
- Pointer Operations: Manipulating pointers, array-pointer relationships, and dynamic memory allocation (e.g., malloc and free).
- Data Structures: Declarations involving structures, unions, and linked lists.
- Function Behavior: Analyzing parameter passing (call-by-value vs. call-by-reference) and recursion, including stack activation records and scope rules.
- Memory Management: Calculating memory requirements for variables, considering alignment and union sizes.
C Programming Gate Questions
1. In the following C function, let n ≥ m.
int gcd(n, m) { if (n % m == 0) return m; n = n % m; return gcd(m, n); }
How many recursive calls are made by this function?
- Θ(log2n)
- Ω(n)
- Θ(log2log2n)
- Θ(√n)
Answer
Θ(log2log2n)
[2007]
2. What is the time complexity of the following recursive function?
int DoSomething(int n) { if (n <= 2) return 1; else return (DoSomething(floor(sqrt(n))) + n); }
- Θ(n2)
- Θ(nlog2n)
- Θ(log2n)
- Θ(log2log2n)
Answer
Θ(log2log2n)
[2007]
3. Choose the correct option to fill ? 1 and ? 2 so that the program below prints an input string in reverse order.
Assume that the input string is terminated by a newline character.
void reverse(void) { int c; if ( ? 1) reverse(); ? 2 } main() { printf(“Enter Text”); printf(“\n”); reverse(); printf(“\n”); }
- ?1 is (getchar( )! = ‘\n’)
?2 is getchar(c); - ?1 is (c = getchar( ))! = ‘\n’)
?2 is getchar(c); - ?1 is (c! = ‘\n’)
?2 is putchar(c); - ?1 is ((c = getchar( ))! = ‘\n’)
?2 is putchar(c);
Answer
?1 is ((c = getchar( ))! = ‘\n’)
?2 is putchar(c);
[2008]
4. Consider the program below:
#include < stdio.h > int fun(int n, int * f_p) { int t, f; if (n <= 1) { f_p = 1; return 1; } t = fun(n - 1, f_p); f = t + f_p; * f_p = t; return f; } int main() { int x = 15; printf(“ % d\ n”, fun(5, & x)); return 0; }
The value printed is
- 6
- 8
- 14
- 15
Answer
8
[2009]
5. What is the value printed by the following C program?
#include<stdio.h> int f(int a, int n) { if (n <= 0) return 0; else if (a % 2 = = 0) return * a + f(a + 1, n– 1); else return * a– f(a + 1, n– 1); } int main() { int a[] = { 12, 7, 13, 4, 11, 6 }; printf(“ % d”, f(a, 6)); return 0; }
- -9
- 5
- 15
- 19
Answer
15
[2010]
6. Consider the following recursive C function that takes two arguments.
unsigned int foo(unsigned int n, unsigned int r) { if (n > 0) return ((n % r) + foo(n / r, r)); else return 0; }
What is the return value of the function foo when it is called as foo (513, 2)?
- 9
- 8
- 5
- 2
Answer
2
[2011]
7. Consider the following recursive C function that takes two arguments.
unsigned int foo(unsigned int n, unsigned int r) { if (n > 0) return ((n % r) + foo(n / r, r)); else return 0; }
What is the return value of the function foo when it is called as foo (345, 10)?
- 345
- 12
- 5
- 3
Answer
12
[2011]
8. Consider the following C code segment
int a, b, c = 0; void prtFun(void); main() { static int a = 1; prtFun(); a + = 1; prtFun(); printf(“\n % d % d”, a, b); } void prtFun(void) { static int a = 2; int b = 1; a + = ++b; printf(“\n % d % d”, a, b); }
What output will be generated by the given code segment if:
Line 1 is replaced by auto int a = 1;
Line 2 is replaced by register int a = 2;
- 3 1
4 1
4 2 - 4 2
6 1
61 - 4 2
6 2
2 0 - 4 2
4 2
2 0
Answer
4 2
4 2
2 0
[2012]
9. Consider the following C code segment
int a, b, c = 0; void prtFun(void); main() { static int a = 1; prtFun(); a + = 1; prtFun(); printf(“\n % d % d”, a, b); } void prtFun(void) { static int a = 2; int b = 1; a + = ++b; printf(“\n % d % d”, a, b); }
What output will be generated by the given code segment?
- 3 1
4 1
4 2 - 4 2
6 1
6 1 - 4 2
6 2
2 0 - 3 1
5 2
5 2
Answer
4 2
6 2
2 0
[2012]
10. What is the return value of f(p, p), if the value of p is initialized to 5 before the call? Note that the first parameter is passed by reference, whereas the second parameter is passed by value.
int f(int & x, int c) { c = c– 1; if (c == 0) return 1; x = x + 1; return f(x, c) * x; }
- 3024
- 6561
- 55440
- 161051
Answer
6561
11. 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
- Half of the product of the three consecutive integers.
- One-third of the product of the three consecutive integers.
- One-sixth of the product of the three consecutive integers.
- None of the above.
Answer
One-sixth of the product of the three consecutive integers.
[2014]
12. Consider the function func shown below:
int func(int num) { int count = 0; while (num) { count++; num >>= 1; } return (count); }
The value returned by func(435) is _______ .
- 7
- 8
- 9
- 10
Answer
9
[2014]
13. Consider the following function
double f(double X) if (abs(X * X– 3) < 0.01) return X; else return f(X / 2 + 1.5 / X); }
Give a value q(to two decimals) such that f(q) will return q:________
- 1.52 to 1.54
- 1.62 to 1.64
- 1.72 to 1.74
- 1.82 to 1.84
Answer
1.72 to 1.74
[2014]
14. Consider the following pseudo-code, where x and y are positive integers
begin q: = 0 r: = x while r≥ y do begin r: = r– y q: = q + 1 end end
The postcondition that needs to be satisfied after the program terminates is
- {r = qx + y ∧ r < y}
- {x = qy + r ∧ r < y}
- {y = qx + r ∧ 0 < r < y}
- {q + 1 < r – y ∧ y > 0}
Answer
{x = qy + r ∧ r < y}
[2015]
15. Consider the following C function
int fun(int n) { int x = 1, k; if (n == 1) return x; for (k = 1; k < n; ++k) x = x + fun(k) * fun(n– k); return x; }
The return value of fun(5) is _________.
- 50
- 51
- 53
- 54
Answer
51
[2015]
16. Consider the following recursive C function
void get(int n) { if (n < 1) return; get(n– 1); get(n– 3); printf(“ % d”, n); }
If get (6) function is being called in main ( ) then how many times will the get ( ) function be invoked before returning to the main ( )?
- 15
- 25
- 35
- 45
Answer
25
[2015]
17. Consider the following C program
#include<stdoio.h> int f1(void); int f2(void); int f3(void); int x = 10; int main() { int x = 1; x += f1() + f2() + f3() + f2(); printf(“ % d”, x); return 0; } int f1() { int x = 25; x++; return x; } int f2() { static int x = 50; x++; return x; } int f3() { x *= 10; return x; }
The output of the program is _______ .
- 225
- 230
- 235
- 240
Answer
230
[2015]
18. Suppose c = < c[0],…,c[k – 1]> is an array of length k, where all the entries are from the set {0, 1}. For any positive integers a and n, consider the following pseudo code.
DOSOMETHING(c, a, n) z← 1 for i← 0 to k– 1 do z← z2 mod n if c[i] = 1 then z←(z× a) mod n return z
If k = 4, c = <1, 0, 1, 1>, a = 2 and n = 8, then the output of DOSOMETHING(c, a, n) is _________ .
- 0
- 1
- 2
- 3
Answer
0
[2015]
19. 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); }
- 3 1 2 2 1 3 4 4 4
- 3 1 2 1 1 1 2 2 2
- 3 1 2 2 1 3 4
- 3 1 2 1 1 1 2
Answer
3 1 2 2 1 3 4 4 4
[2016]
20. The following function computes XY for positive integers X and Y.
int exp(int X, int Y) { int res = 1, a = X, b = Y; while (b! = 0) { if (b % 2 = = 0) { a = a * a; b = b / 2; } else { res = res * a; b = b - 1; } } return res; }
Which one of the following conditions is TRUE before every iteration of the loop?
- XY = ab
- (res *a)Y = (res *X)b
- XY = res *ab
- XY = ( res *a)b
Answer
XY = res *ab
[2016]
21. 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
- 53423122233445
- 53423120112233
- 53423122132435
- 53423120213243
Answer
53423122233445
[2017]
22. Consider the C functions foo and bar given below:
int foo(int val) { int x = 0; while (val > 0) { x = x + foo(val−−); } return val; } int bar(int val) { int x = 0; while (val > 0) { x = x + bar(val− 1); } return val; }
Invocations of foo (3) and bar (3) will result in:
- Return of 6 and 6 respectively.
- Infinite loop and abnormal termination respectively.
- Abnormal termination and infinite loop respectively.
- Both terminating abnormally.
Answer
Abnormal termination and infinite loop respectively.
[2017]
23. 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); }
- 20
- 23
- 25
- 30
Answer
23
[2017]
24. Consider the following C program:
#include<stdio.h> int counter = 0; int calc(int a, int b) { int c; counter++; if (b == 3) return (aaa); else { c = calc(a, b / 3); return (ccc); } } int main() { calc(4, 81); printf(“ % d”, counter); }
The output of this program is ______.
- 1
- 2
- 3
- 4
Answer
4
[2018]
25. Consider the following program written in pseudocode. 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 ___________.
- 11253
- 9207
- 10230
- 8184
Answer
10230
[2018]
26. Which one of the following are essential features of an object-oriented programming language?
(i) Abstraction and encapsulation
(ii) Strictly-typedness
(iii) Type-safe property coupled with sub-type rule
(iv) Polymorphism in the presence of inheritance
- (i) and (ii) only
- (i) and (iv) only
- (i), (ii) and (iv) only
- (i), (iii) and (iv) only
Answer
(i) and (iv) only
[2005]
27. Which of the following are true?
(i) A programming language which does not permit global variables of any kind and has no nesting of procedures/functions, but permits recursion can be implemented with static storage allocation.
(ii) Multi-level access link (or display) arrangement is needed to arrange activation records only if the programming language being implemented has nesting of procedures/functions.
(iii) Recursion in programming languages cannot be implemented with dynamic storage allocation.
(iv) Nesting procedures/functions and recursion require a dynamic heap allocation scheme and cannot be implemented with a stack-based allocation scheme for activation records.
(v) Programming languages which permit a function to return a function as its result cannot be implemented with a stack-based storage allocation scheme for activation records.
- (ii) and (v) only
- (i), (iii) and (iv) only
- (i), (ii) and (v) only
- (ii), (iii) and (v) only
Answer
(ii), (iii) and (v) only
[2008]
28. What will be the output of the following C program segment?
char inChar = ‘A’; switch (inChar) { case‘ A’: printf(“choice A\ n”): case‘ B’: case‘ C’: printf(“choice B”); case‘ D’: case‘ E’: default: printf(“No Choice”); }
- No choice
- Choice A
- Choice A
Choice B No choice - Program gives no output as it is erroneous
Answer
Choice A
Choice B No choice
[2012]
29. Suppose n and p are unsigned int variables in a C program. We wish to set p to nC3. If n is large, which one of the following statements is most likely to set p correctly?
- p = n * (n – 1) * (n – 2)/6;
- p = n * (n – 1) /2* (n – 2)/3;
- p = n * (n – 1) /3 * (n – 2)/2;
- p = n * (n – 1) * (n – 2)/6.0;
Answer
p = n * (n – 1) /2* (n – 2)/3;
[2014]
30. The secant method is used to find the root of an equation f(x) = 0. It is started from two distinct estimates xa and xb for the root. It is an iterative procedure involving linear interpolation to a root. The iteration stops if f(xb) is very small and then xb is the solution. The procedure is given below. Observe that there is an expression which is missing and is marked by ?. Which is the suitable expression that is to put in place of ? so that it follows all steps of the secant method?
- xb-(fb-f(xa))fb/(xb-xa)
- xa-(fa-f(xa))fb/(xb-xa)
- xb-(xb-xa)fb/(fb-f(xa))
- xa-(xb-xa)fa/(fb-f(xa))
Answer
xb-(xb-xa)fb/(fb-f(xa))
[2015]
31. Consider the following C program:
#include<stdio.h> int main() { int i, j, k = 0; j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5; k -= --j; for (i = 0; i < 5; i++) { switch (i + k) { case 1: case 2: printf(“\n % d”, i + k); case 3: printf(“\n % d”, i + k); default: printf((“\n % d”, i + k); } } return 0; }
The number of times printf statement is executed is _______.
- 8
- 10
- 12
- 14
Answer
10
[2015]
32. 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.
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)?
- (q == r) && (r == 0)
- (x > 0) && (r == x) &&(y > 0)
- (q == 0) && (r == x) && (y > 0)
- (q == 0) && (y > 0)
Answer
(q == 0) && (r == x) && (y > 0)
[2017]
33. Consider the following C Program.
#include<stdio.h> int main() { int m = 10; int n, nl; n = ++m; nl = m++; n−−;−− nl; n− = nl; printf(“ % d”, n); return 0; }
The output of the program is __________ .
- 0
- 1
- 2
- 3
Answer
0[2017]