Previous GATE Questions With Solutions On C Programming - CS/IT
Previous GATE Questions With Solutions On C Programming - CS/IT
Previous GATE Questions With Solutions On C Programming - CS/IT
GATE-2000
is
Explanation:
The smallest individual units are known as C Tokens. The keywords, identifiers, constants, string
literals, and operators described in this section are examples of tokens. Punctuation characters such
as brackets ([ ]), braces ({ }), parentheses ( ( ) ), and commas (,) are also tokens.
Example:
1) printf
2) (
4) )
5) ;
10 Tokens are:
1) printf
2) (
4) ,
5) i
6) ,
7) &
8) i
9) )
10);
GATE-2012
switch (inchar)
case 'A' :
case 'B' :
case 'D' :
case 'E' :
default:
(a) No choice
(b) Choice A
(c) Choice A
Choice B No choice
Explanation:
In switch statement if a case is executed and if the case doesn't contain break, then all the subsequent 'case'
statements are executed until a break statement is found. If I modify the above code as below:
switch (inchar)
case 'A' :
case 'B' :
case 'C' :
case 'D' :
case 'E' :
default:
printf ("No Choice") ;
GATE-1999
Ans: (c)
Explanation:
The first condition, (a > = b) && (c < b), if true will return the middle number, i.e. b. Again on calling
count = count + i;
return (count);
main ()
int i,j;
j = incr(i);
Explanation:
count variable is declared as static in incr function. Therefore, static int count = 0;, will assign zero to
the count variable in the first call only. When the incr function is called for 2nd, 3rd or 4th times,
count variable will hold its previous value, and will not be re-initialized to zero again.
Static variables hold their previous values, and they do not re-initialized when the function is called
GATE CS 2002
Assuming that the main memory is byte-addressable and that the array is stored starting from memory address
Explanation:
= 0 + 40 x 100 x 1 + 50 x 1 = 4050
GATE CS 2005
int k = 0, j = 0;
if (n == 0) return;
k = n % 10; j = n / 10;
sum = sum + k;
int main ()
getchar();
(a) 8, 4, 0, 2, 14
(b) 8, 4, 0, 2, 0
(c) 2, 0, 4, 8, 14
(d) 2, 0, 4, 8, 0
going through each code, you will find that foo function prints all the digits of a number stored in variable a.
Note:
i) k= n % 10 => modulus operator will return the remainder. for example, if n=2048, then 2048 %10 will store
the value 8 in k.
ii) j is declared as integer datatype in foo function. Therefore after division if the result contains decimal part,
it will be truncated and only the integer part will be stored in j. For example if j=2048/10, (actual result =
int f(int n)
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
a) 5 b) 6 c) 7 d) 8
Explanation:
Note that i is declared as static variable, first line of the function, static int i = 1;, will be executed only once.
Static variables hold their previous values, and they do not re-initialized when the function is called again. The
GATE CS 2004
main()
{
int x, y, m, n;
m = x; n = y;
while (m != n)
if(m>n)
m = m - n;
else
n = n - m;
printf("%d", n);
Explanation:
Give some values to x and y and work out the code step by step. The program code is an implementation
of Euclid's algorithm which is an efficient method for computing the greatest common divisor (GCD) of two
integers.
int main () {
// input da
db = foo (da);
return a;
The above code compiled without any error or warning. If Line 1 is deleted, the above code will show:
Error:
GATE-2005
int i, n; /* Line 2 */
Identify the compiler’s response about this line while creating the object-module:
(c) Only syntactic errors (d) Both lexical and syntactic errors
Ans: option(c)
GATE-2006
11. Consider these two functions and two statements S1 and S2 about them.
int work1(int *a, int i, int j) int work2(int *a, int i, int j)
{ {
int x = a[i+2]; int t1 = i+2;
a[j] = x+1; int t2 = a[t1];
return a[i+2] – 3; a[j] = t2+1;
} return t2 – 3;
}
S1: The transformation from work1 to work2 is valid, i.e., for any program state and input arguments, work2
will compute the same output and have the same effect on program state as work1
S2: All the transformations applied to work1 to get work2 will always improve the performance (i.e reduce
Explanation:
In S2 it is asking about improvement in performance i.e. reduction in CPU time. When compared work2 will
reduce the CPU time, because in work1 a[i+2] is computed twice but in work2 a[i+2] is computed once and
stored in t2, and then t2 is used. When we consider the performance in terms of reduction in CPU time, S2 is
correct.
GATE - 2007
int f(int n)
static int r = 0;
if (n <= 0) return 1;
if (n > 3)
r = n;
return f(n-2)+2;
return f(n-1)+r;
Explanation:
As explained before, Static variables retain their values throughout the life of the program. The steps involved
Since f(3) returns value 16, the final step is 16+2 = 18. Therefore f(5) will return value 18.
GATE-2011
13. What is the return value of the function foo when it is called as foo(345, 10) ?
Explanation:
foo(345,10) = 5 + [ foo(34,10) ]
= 5 + [ 4 + { foo(3,10) } ]
= 5 + [ 4 + { 3 + foo(0,10) } ] = 5 + 4 + 3 + 0 = 12
14. What is the return value of the function foo when it is called as foo(513, 2)?
Explanation:
Executing as above we get the value returned as 2. Here, foo function returns sum of bits of a number n,
because r = 2.
GATE-2008
15. Choose the correct option to fill ?1 and ?2 so that the program below prints an input string in reverse order.
void reverse(void)
{
int c;
if (?1) reverse() ;
?2
main()
?2 is getchar(c);
?2 is getchar(c);
(c) ?1 is (c != ’\n’)
?2 is putchar(c);
?2 is putchar(c);
Explanation:
The reverse function should read the character you type until you press the "enter" key. Hence before
printing, reverse function is called again and again until "enter" key is pressed. Once the "enter" key is
pressed; the functions from the function stack run putchar(c) statements one by one. Therefore, last entered
GATE-2000
struct node
int i;
float j;
};
define s to be
GATE-2000
is:
Explanation:
malloc(size) - malloc function allocates a block of size bytes from the free data area (heap). On success, malloc
Therefore after execution of m=malloc(5);,m contains a pointer to the newly allocated memory. But m=NULL;
statement will store the NULL value to m, without freeing the memory. Since the allocated memory is not yet
free - deallocates a memory block allocated previously by malloc, calloc, or realloc. Here, "free(n); n-
>value=5;", n is trying to retrieve a value after the memory has freed, hence dangling pointer.
GATE CS 2000
struct {
short s [5]
union {
float y;
long z;
}u;
} t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The
Explanation:
The amount of memory required to store a structure variable is the sum of the sizes of all its
members. But in the case of union, the amount of memory required is the amount required by its
largest member.
Therefore u, which is a union member of the struct, occupies only 8 bytes of memory, because the
largest memory is 8 bytes consumed by long z;. Another member in the struct is short s [5], this will
GATE CS 2003
I. A[2]
II. A[2][3]
III. B[1]
IV. B[2][3]
which will not give compile-time errors if used as left hand sides of assignment statements in a C program?
(d) IV only
Explanation:
A is an array of pointers, and A[2] can be used as left hand sides of assignment statements.
Suppose we have another array of integers i.e. int marks[]={10,20,30,40}. Then we can assign A[2] =
marks; Because marks represents the starting address of the array marks[], and on execution, the
Considering the assignment A[2] = marks;, A[2][3] represents the element 40 (i.e. 4th element in the
marks array). Therefore, A[2][3] also can be used as left hand sides of assignment statements. i.e.
B[1] cannot be used as left hand sides of assignment statements, because, since it is a two-
GATE CS 2004
{
int temp;
temp = a;
a = b;
b = temp;
Explanation:
call swap (x, y) - will swap only the formal parameters (i.e. a and b). The value actual parameters
(i.e. x and y) will remain the same, because the value are passed by CALL BY VALUE.
call swap (&x, &y) - will not work because, swap function accepts VALUES as parameters, but we
option (c): swap (x,y) cannot be used, it is correct, but reason is not correct.
GATE-2005
int ( * f) (int * ) ;
(a) A function that takes an integer pointer as argument and returns an integer
(b) A function that takes an integer as argument and returns an integer pointer
(c) A pointer to a function that takes an integer pointer as argument and returns an integer.
(d) A function that takes an integer pointer as argument and returns a function pointer
Explanation:
GATE-2010
#include<stdio.h>
p = q;
*p = 2;
int i = 0, j = 1;
int main()
f(&i, &j);
getchar();
return 0;
}
(a) 2 2 (b) 2 1 (c) 0 1 (d) 0 2
Explanation:
*p = 2; 2 value is saved in the location where the p is pointing. Since p points to j, value of j is changed to 2
now.
Now returns to main function and prints i and j.Hence i=0 and j=2
GATE-2011
char *p =c;
Explanation:
p = base address of C
GATE CS 2004
char p[20];
char *s = "string";
int i;
printf("%s",p);
(a) gnirts
(b) gnirt
(c) string
Explanation:
Therefore p[0]='\0'
Hence during the execution of the printf statement since the very first character is the null character, it assumes
GATE-2012
int a, b, c = 0;
void prtFun (void);
int main ()
{
static int a = 1; /* line 1 */
prtFun();
a += 1;
prtFun();
printf ( "\n %d %d " , a, b) ;
}
3 1 4 2 4 2 3 1
4 1 6 1 6 2 5 2
4 2 6 1 2 0 5 2
Explanation:
When a function encounters a variable, it will first check for its local variable. If local variable not found then
it will go for global variable. In the above program we can see that there are 3 global variable a,b, & c. But in
prtFun() we have local variable a & b. Therefore it will not go for the global variable a & b. But in the main
function we have only a as the local variable, therefore the print statement in main() will take the value of
31 42 42 42
41 61 62 42
42 61 20 20
static variable are now made as non-static, therefore each time when the function is called the variables are
again initialized.
Note that the register variables are same as auto variables, the only difference is that the register variables are
allocated iin the registers, instead of main memory, for faster access.
GATE-2008
void main()
{
int c, *b, **a;
c = 4;
b = &c;
a = &b;
printf( "%d", f(c,b,a));
getchar();
}
Ans: option(b)
Hint: pointer b and py is pointing to c
Initially c = 4
ppz contains the address of b, therefore **ppz refers to the value to which b is pointing. Since b is
pointing towards c, the value of c increments when **ppz += 1; statement executes. Therefore c now
becomes 5.
z=**ppz; means z = 5
y=*py; means y = 7
GATE - 2015
#include <stdio.h>
int c;
int c;
c=*a; *a=*b;*b=c;
}
int main()
f1 (a, b);
f2 (&b, &c);
printf(“%d”, c-a-b);
Ans: -5
Explanation:
You can see that the f1 function is calls its parameters by value. Hence the modifications are made
inside the scope of f1 functions does not effect any of the variables of main function. But in f2, the
In f2, we can see that pointer a points to variable b and pointer b points to variable c.
*b = c; // means *b = 5; that is c = 5;
GATE - 2015
29. What is the output of the following C code? Assume that the address of x is 2000 (in decimal)
int main()
{
unsigned int x[4][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}};
(d) 2012, 4, 6
Explanation:
Here x is a two-dimensional array. Also it is given that the base address of x = 2000. And the integer
requires 4 bytes of memory. x[4][3] means that x has 4 rows and each row has 3 integers.
x + 3 means address of the 4th row (i.e. row with integers 10,11,12)
= 2000 + 3 * (3 * 4)
= 2036
*(x + 3) => Since x is a two-dimensional array one reference operator (*) will only return the starting
address of the one-dimensional array. i.e. *(x + 3) will return the starting address of the 4th-row,
*(x + 2) + 3 means the address of 4th-integer of 3rd-row [ *(x + 2) means address of 3rd-row ]
=> 2024 + 12
=> 2036
GATE - 2015
int i, j, k, p, q = 0;
p = 0;
++p;
++q;
return q;
Which one of the following most closely approximates the return value of the function fun1?
Explanation:
We can see that the first(outer) 'for' loop [i.e. for (i = 1; i<n; ++i)] runs for O(n) times.
Inner loop has two 'for' loops (j & k). 'j' loop runs for O(log n) times. 'k' loop runs for O(log p) times.
But if we iterate through the question again, we need to find the approximate value that is returned
by the function fun1 (i.e. value of q). As we saw above, value of q depends on p. On each iteration of
'i' loop, value of q increases by O(log p). And since p = log n, the value of q on each iteration of i
increases by Θ(log(log n)). Hence nlog(log n) most closely approximates the return value of the
function fun1.
GATE - 2015
#include <stdio.h>
int main( )
p = s1 + 2;
*p = ‘0’ ;
(a) 12
(b) 120400
(c) 1204
(d) 1034
Explanation:
p = s1 + 2; means that p holds the address of character 3. [s1 gives the address of character 1,
s1+1 gives the address of character 2, s1+2 gives the address of character 3, so on ...]
GATE - 2015
#include<stdio.h>
int main( )
int **ptr = p;
ptr++;
Ans: 140
Explanation:
When we increment a pointer (like ptr++; in our question) it adds the size of the data type. Hence, it
moves to the next cell of p, which in turn points to the 4th cell of a. Hence **ptr gives us the value 40.
According to Pointer Subtraction, we can subtract two pointers of the same type. The result is the
Refer: https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/BitOp/pointer.html
ptr - p = 1
GATE-2015
get (n–1);
get (n–3);
If get(6) function is being called in main( ) then how many times will the get() function be invoked
(a) 15
(b) 25
(c) 35
(d) 45
Explanation:
GATE - 2015
# include <stdio.h>
int main( )
int i, j, k = 0;
j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
k –= ––j;
switch(i + k)
{
case 1:
}
}
return 0;
Ans: 10
Explanation:
According to the precendence of operators we know that * & / has same precedence. Hence we
need to evaluate them in left to right associativity. Also we know that, * & / has higher precedence
j = 2 * 3 / 4 + 2.0 / 5 + 8 / 5;
= 1 + 0.4 + 1
= 2.4
=2
Note1: 8/5 = 1 rather than 1.6 because both are integers. Hence result also remains integer by
Note2: 2.0/5 = 0.4 because since 2.0 is of type double 5 is implicitly typecast to double.
Note3: j = 2.4 = 2 since j is of integer type implicit typecast takes place. Hence 2.4 becomes 2.
So after evaluation, we get j = 2 and k = -1
i = 0; Hence switch (-1) => Since no case is defined for the value of -1, we go to the default case.
i = 1; switch (0) => printf will be executed only once since its the default case again.
i = 2; switch (1) => Now since case 1 is defined it moves to the case 1 block but since break is not
specified in any case, case1, case2, case3, and default case is executed. Hence printf statement is
i = 3; switch (2) => Now since case-2 is defined it moves to the case2 block but since break is not
specified case2, case3, and default case is executed. Hence printf statement is executed 3 times
when i = 3.
i = 4; switch (3) => Now since case-3 is defined it moves to the case3 block but since break is not
specified case3, and default case is executed. Hence printf statement is executed 2 times when i =
4.
GATE - 2015
#include<stdio.h>
int f1(void);
int f2(void);
int f3(void);
int x = 10;
int main( )
int x = 1;
printf(“%d”, x);
return 0;
Ans: 230
Explanation:
= 1 + f1() + f2() + f3() + f2(); //x=1 since there is a local declaration of x in main function.
= 1 + 26 + 51 + 100 + 52
= 230
Note that in f2() x is declared as static, which means that x will be initialized only once and will retain
its value between invocations. Hence first call of f2() returns 51 and second call of f2() returns 52.
In f3(), x = x * 10; Since there is no local declaration of x in f3(), we get the value of x as 10, from the
global declaration.
GATE - 2015
#include<stdio.h>
switch (opcode)
{
}
return –1;
int main()
stkFunc(–1, 10);
stkFunc(0, 5);
stkFunc(0, 10);
Ans: 15
Explanation:
explanation of question 35 to know more on static variable). Hence A[0] = 5, and stkTop = 1.
stkFunc(0, 10); // again case 0. Now stkTop = 1 and size = 10. Hence A[1]=10 and stkTop = 2;
printf(“%d\n”, stkFunc (1, 0) + stkFunc (0, 0)); // prints 15. Since stkFun(1,0) and
GATE-2015
int fun(int n)
int x=1, k;
return x;
Ans: 51
Explanation:
Recurrence Relation
fun(5) = 1 + fun(1) * fun(4) + fun(2) * fun(3) + fun(3) * fun(2) + fun(4) * fun(1)
= 1 + 15 + 10 + 10 + 15
= 51
GATE - 2015
{
foo (a+1);
putchar (*a);
}
(b) ABCD
(d) DCBA
Explanation:
Note that there is a space after ABCD, in the input. Therefore input is ABCD<space>EFGH.
The program prints all characters before the space or ‘\0′ (whichever comes first) in reverse order.
GATE-1991
if i mod 2 = 0 then
while i >= 0 do
begin
i := i div 2;
else i := i – 2;
end;
An appropriate loop-invariant for the while-loop is ________
Ans: i mod 2 = 0
Explanation:
A loop invariant is a formal statement about the relationship between variables in our program which
holds true just before the loop is ever run and is true again at the bottom of the loop, each time
Here is the general pattern of the use of Loop Invariant in your code:
...
...
}
...
Ref: http://www.cs.miami.edu/home/burt/learning/Math120.1/Notes/LoopInvar.html