Data Structure
Data Structure
(2012)
(2012)
GATE MATERIAL
(2012)
GATE MATERIAL
61
(C) 4 2
62
20
(D) 4 2
42
20
Answer (D)
If we replace line 1 by auto int a = 1; and line 2 by register int a = 2;, then a becomes non-static in
prtFun(). The output of first prtFun() remains same. But, the output of second prtFun() call is changed as a new
instance of a is created in second call. So 4 2 is printed again. Finally, the printf() in main will print 2 0.
Making a a register variable wont change anything in output.
4. What does the following fragment of C-program print?
char c[] = "GATE2011";
char *p =c;
printf("%s", p + p[3] - p[1]) ;
(A) GATE2011
(B) E2011
(C) 2011
(D) 011
Answer: (C)
See comments for explanation.
char c[] = "GATE2011";
(2011)
GATE MATERIAL
is 1 + 0 + 0. + 0 + 1.
The function foo(n, 2) basically returns sum of bits (or count of set bits) in the number n.
6. What is the return value of the function foo when it is called as foo(345, 10) ? (2011)
(A) 345
(B) 12
(C) 5
(D) 3
Answer: (B)
The call foo(345, 10) returns sum of decimal digits (because r is 10) in the number n. Sum of digits for 345 is 3
+ 4 + 5 = 12.
Following questions have been asked in GATE CS 2010 exam.
7. What does the following program print?
(2010)
#include<stdio.h>
void f(int *p, int *q)
{
p = q;
*p = 2;
}
int i = 0, j = 1;
int main()
{
f(&i, &j);
printf("%d %d \n", i, j);
getchar();
return 0;
}
(A) 2 2
(B) 2 1
(C) 0 1
(D) 0 2
Answer (D)
See below f() with comments for explanation.
/* p points to i and q points to j */
void f(int *p, int *q)
{
p = q; /* p also points to j now */
*p = 2; /* Value of j is changed to 2 now */
}
8. What is the value printed by the following C program?
(2010)
#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()
{
IT DEPARTMENT
GATE MATERIAL
GATE MATERIAL
c = 4;
b = &c;
a = &b;
printf( "%d", f(c,b,a));
getchar();
}
(A) 18
(B) 19
(C) 21
(D) 22
Answer (B)
/* Explanation for the answer */
/*below line changes value of c to 5. Note that x remains unaffected
by this change as x is a copy of c and address of x is different from c*/
**ppz += 1
/* z is changed to 5*/
z = **ppz;
/* changes c to 7, x is not changed */
*py += 2;
/* y is changed to 7*/
y = *py;
/* x is incremented by 3 */
x += 3;
/* return 7 + 7 + 5*/
return x + y + z;
10. 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.
(2008)
void reverse(void)
{
int c;
if (?1) reverse() ;
?2
}
main()
{
printf ("Enter Text ") ;
printf ("\n") ;
reverse();
printf ("\n") ;
}
(A) ?1 is (getchar() != \n)
?2 is getchar(c);
IT DEPARTMENT
GATE MATERIAL
GATE MATERIAL
}
return X[n] ;
}
11. The running time of f1(n) and f2(n) are
(2008)
(A) (n) and (n)
(B) (2^n) and (n)
(C) (n) and (2^n)
(D) (2^n) and (2^n)
Answer (B)
For f1(), let T(n) be the function for time complexity.
T(n) = T(n-1) + T(n-2)
Above recursion is a standard one for Fibonacci Numbers. After solving the recursion, we get
T(n) = 1/sqrt(5)[(1 + sqrt(5))/2]^n - 1/sqrt(5)[(1 - sqrt(5))/2]^n
Above recursion can also be written as (1.618.^n)
(Please see this).
In f2(), there is a single loop, so time complexity is (n)
Among all the 4 given choices, (B) looks closest.
GATE MATERIAL
14. Consider the following C-function in which a[n] and b[m] are two sorted integer arrays and c[n +
m] be another integer array.
2006
void xyz(int a[], int b [], int c[])
{
int i, j, k;
i = j = k = O;
while ((i<n) && (j<m))
if (a[i] < b[j]) c[k++] = a[i++];
else c[k++] = b[j++];
}
Which of the following condition(s) hold(s) after the termination of the while loop?
(i) j < m, k = n+j-1, and a[n-1] < b[j] if i = n
(ii) i < n, k = m+i-1, and b[m-1] <= a[i] if j = m
(A) only (i)
(B) only (ii)
(C) either (i) or (ii) but not both
(D) neither (i) nor (ii)
Answer (C)
The condition (i) is true if the last inserted element in c[] is from a[] and condition (ii) is true if the last inserted
element is from b[].
15. Consider this C code to swap two integers and these five statements: the code
2006
void swap(int *px, int *py)
{
*px = *px - *py;
*py = *px + *py;
*px = *py - *px;
}
S1: will generate a compilation error
S2: may generate a segmentation fault at runtime depending on the arguments passed
S3: correctly implements the swap procedure for all input pointers referring to integers stored in memory
locations accessible to the process
S4: implements the swap procedure correctly for some but not all valid input pointers
S5: may add or subtract integers and pointers.
(A) S1
(B) S2 and S3
(C) S2 and S4
(D) S2 and S5
Answer (C)
S2: May generate segmentation fault if value at pointers px or py is constant or px or py points to a memory
location that is invalid
S4: May not work for all inputs as arithmetic overflow can occur.
IT DEPARTMENT
GATE MATERIAL
GATE MATERIAL
int i;
for (i = 0; i < length; i++)
p[i] = s[length i];
printf("%s",p);
The output of the program is
a) gnirts
b) gnirt
c) string
d) no output is printed
Answer(d)
Let us consider below line inside the for loop
p[i] = s[length i];
For i = 0, p[i] will be s[6 0] and s[6] is \0
So p[0] becomes \0. It doesnt matter what comes in p[1], p[2].. as P[0] will not change for i >0. Nothing is
printed if we print a string with first character \0
19. Consider the following C function
void swap (int a, int b)
(GATE CS 2004)
{
int temp;
temp = a;
a = b;
b = temp;
}
In order to exchange the values of two variables x and y.
a) call swap (x, y)
b) call swap (&x, &y)
c) swap (x,y) cannot be used as it does not return any value
d) swap (x,y) cannot be used as the parameters are passed by value
Answer(d)
Why a, b and c are incorrect?
a) call swap (x, y) will not cause any effect on x and y as parameters are passed by value.
b) call swap (&x, &y) will no work as function swap() expects values not addresses (or pointers).
c) swap (x, y) cannot be used but reason given is not correct.
20. Consider the following C function:
int f(int n)
(GATE CS 2004)
{
static int i = 1;
if (n >= 5)
return n;
n = n+i;
i++;
return f(n);
}
The value returned by f(1) is
a) 5
b) 6
c) 7
d) 8
IT DEPARTMENT
GATE MATERIAL
Answer (c)
Since i is static, first line of f() is executed only once.
Execution of f(1)
i=1
n=2
i=2
Call f(2)
i=2
n=4
i=3
Call f(4)
i=3
n=7
i=4
Call f(7)
since n >= 5 return n(7)
21. Consider the following program fragment for reversing the digits in a given integer to obtain a
new integer. Let n = D1D2Dm
int n, rev;
rev = 0;
(GATE CS 2004)
while (n > 0)
{
rev = rev*10 + n%10;
n = n/10;
}
The loop invariant condition at the end of the ith iteration is:
a) n = D1D2.Dm-i and rev = DmDm-1Dm-i+1
b) n = Dm-i+1Dm-1Dm and rev = Dm-1.D2D1
c) n rev
d) n = D1D2.Dm and rev = DmDm-1D2D1
Answer (a)
22. Consider the following C program
main()
(GATE CS 2004)
{
int x, y, m, n;
scanf ("%d %d", &x, &y);
/* x > 0 and y > 0 */
m = x; n = y;
while (m != n)
{
if(m>n)
m = m - n;
else
n = n - m;
}
printf("%d", n);
}
The program computes
a) x + y using repeated subtraction
IT DEPARTMENT
GATE MATERIAL
GATE MATERIAL
j = incr(i);
}
(a) 10
(b) 4
(c) 6
(d) 7
Answer (a)
Eplaination: count is static variable in incr(). Statement static int count = 0 will assign count to 0 only in first
call. Other calls to this function will take the old values of count.
Count will become 0 after the call incr(0)
Count will become 1 after the call incr(1)
Count will become 3 after the call incr(2)
Count will become 6 after the call incr(3)
Count will become 10 after the call incr(4)
25. Consider the following C declaration
(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 memory requirement for variable t, ignoring alignment
considerations, is
(a) 22 bytes
(b) 14 bytes
(c) 18 bytes
(d) 10 bytes
Answer: (c)
Explanation: Short array s[5] will take 10 bytes as size of short is 2 bytes. Since u is a union, memory
allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8).
26. The number of tokens in the following C statement.
(GATE 2000)
printf("i = %d, &i = %x", i, &i);
is
(a) 3
(b) 26
(c) 10
(d) 21
Answer (c)
Explanation:In a C source program, the basic element recognized by the compiler is the token. A token is
source-program text that the compiler does not break down into component elements.
There are 6 types of C tokens : identifiers, keywords, constants, operators, string literals and other separators.
There are total 10 tokens in the above printf statement.
IT DEPARTMENT
GATE MATERIAL
GATE MATERIAL
GATE MATERIAL
int main ()
{
int a = 2048, sum = 0;
foo (a, sum);
printf ("%d\n", sum);
getchar();
}
What does the above program print?
(a) 8, 4, 0, 2, 14
(b) 8, 4, 0, 2, 0
(C) 2, 0, 4, 8, 14
(d) 2, 0, 4, 8, 0
Answer (d)
sum has no use in foo(), it is there just to confuse. Function foo() just prints all digits of a number. In main,
there is one more printf statement after foo(), so one more 0 is printed after all digits of n.
33. Consider the following C function definition:
GATE-1999
int Trial (int a, int b, int c)
{
if ((a > = b) && (c < b)) return b;
else if (a > = b) return Trial (a,c,b);
else return Trial (b,a,c);
}
The function Trial:
(a) Finds the maximum of a, b, and c
(b) Finds the minimum of a, b and c
(c) Finds the middle number of a, b, c
(d) None of the above
Ans: (c)
Explanation:
The first condition, (a > = b) && (c < b), if true will return the middle number, i.e. b. Again on calling
the Trial function, it will return the middle number.
34. The value of j at the end of the execution of the following C program.
int incr (int i)
{
static int count = 0;
count = count + i;
return (count);
}
main ()
{
int i,j;
for (i = 0; i <=4; i++)
j = incr(i);
}
(a) 10 (b) 4 (c) 6 (d) 7
IT DEPARTMENT
GATE CS 2000
GATE MATERIAL
GATE-2005
GATE MATERIAL
GATE-2006
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
CPU time) of work2 compared to work1
(a) S1 is false and S2 is false
(b) S1 is false and S2 is true
(c) S1 is true and S2 is false
(d) S1 is true and S2 is true
Ans: option (d)
Explanation:
Both functions work1 & work2 performs the same task, therefore S1 is true.
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.
39. Consider the following C function:
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;
}
What is the value of f(5) ?
(a)5 (b)7 (c)9 (d)18
IT DEPARTMENT
GATE MATERIAL
Since f(3) returns value 16, the final step is 16+2 = 18. Therefore f(5) will return value 18.
40. Consider the following C declaration
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 memory requirement for variable t, ignoring alignment considerations, is
(a) 22 bytes (b) 14 bytes (c) 18 bytes (d) 10 bytes
Ans: option (c)
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
occupy 10 bytes of memory ( 2 bytes x 5).
Hence total 10 + 8 =18 bytes.
GATE-2005
41. What does the following C-statement declare?
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.
IT DEPARTMENT
GATE MATERIAL
(d) A function that takes an integer pointer as argument and returns a function pointer
Ans: option (c)
Explanation:
Syntax to declare pointer to a function => datatype (*pointer_variable)(list of arguments)
To make a pointer to a function => pointer_variable = function_name
Note: don't use parenthesis of the function.
To call (invoke) the function => pointer_variable(list of arguments)
Print statement in main() will take the value of global variable b and the value of local variable a, while
printing.
42. Which one of the following is the tightest upper bound that represents the time complexity of
inserting an object into a binary search tree of n nodes?
(A) O(1)
(B) O(log n)
(C) O(n)
(GATE 2013)
Answer: (C)
Explanation: For skewed binary search tree on n nodes, the tightest upper bound to insert a node is O(n).
43. Which one of the following is the tightest upper bound that represents the number of swaps required
to sort n numbers using selection sort?
(A)O(log n)
(B) O(n)
(D) O(n2)
(GATE 2013)
Answer: (B)
Explanation: The maximum number of swaps that takes place in selection sort on n numbers is n
44. Consider the following operation along with Enqueue and Dequeue operations on queues, where k is
global parameters
MultiDequeue(Q) {
m=k
while (Q is not empty) and (m >0) {
Dequeue(Q)
m=m-1
} }
What is the worst case time complexity of a sequence of n queue operations on an initially empty queue?
(A) (n)
(B) (n+k)
(C) (nk)
(D) (n2)
(GATE 2013)
Answer: (C)
IT DEPARTMENT
GATE MATERIAL
45. The preorder traversal sequence of a binary search tree is 30, 20, 10, 15, 25, 23, 39, 35, 42. Which one
of the following is the postorder traversal sequence of the same tree?
(A) 10,20,15,23,25,35,42,39,30
(B) 15,10,25,23,20,42,35,39,30
(C) 15,20,10,23,25,42,35,39,30
(D) 15,10,23,25,20,35,42,39,30
Answer: (D)
Explanation:
Preorder : 30,20,10,15,25,23,39,35,42
Inorder :10,15,20,23,25,30,35,39,42
46. What is the return value of f p,pif 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;
}
(A) 3024
Answer: (B)
IT DEPARTMENT
(B) 6561
(C) 55440
(D) 161051
(GATE 2013)
GATE MATERIAL
Explanation:
GATE MATERIAL
47. The tester now tests the program on all input strings of length five consisting of characters a, b, c,
d and e with duplicates allowed. If the tester carries out this testing with the four test cases given
above, how many test cases will be able to capture the flaw?
(A) Only one (B) Only two (C) Only three (D) All four
(GATE 2013)
Answer: (B)
Explanation: Flaw in this given procedure is that one character of Array A can be replaced by more than one
character of newc array, which should not be so.Test case (3) and (4) identifies this flaw as they are
containing oldc and newc array characters arranged in specific manner. Following string can reflect flaw, if
tested by test case (3).
Likewise single character b in A is replaced by c and then by d. Same way test case (4) can also catch the
flaw.
48. If array A is made to hold the string abcde, which of the above four test cases will be successful in
exposing the flaw in this procedure?
(A) None
(B) 2 only
(D) 4 only
(GATE 2013)
Answer: (C)
Explanation: Now for string abcde in array A, both test case (3) and (4) will be successful in finding the flaw,
as explained in above question.
8. What will be output of the following program?
char inChar=A;
switch(inChar)
{
case A: print(Choice A\n);
IT DEPARTMENT
GATE MATERIAL
case B:
case C: printf(Choice B);
case D:
case E:
default: printf(No Choice);
}
(A) No Choice
(B) Choice A
(C) Choice A
Choice B No Choice
(D) Program gives no output as it is erroneous
(GATE 2012)
Answer: (C)
Explanation:-Since there is no break statement, the program executes all the subsequent case
statements after printing choice A.
49. Supposeacircularqueueofcapacity(n1)elementsisimplementedwithan
arrayofnelements.AssumethattheinsertionanddeletionoperationsarecarriedoutusingREARandFRONTa
sarrayindexvariables,respectively.Initially,REAR=FRONT=0.Theconditionstodetectqueuefullandqueue
emptyare
(A)full:(REAR+1)modn==FRONT
empty:REAR==FRONT
(B)full:(REAR+1)mod n==FRONT
empty:(FRONT+1)modn==REAR
(C)full:REAR==FRONT(D)full:(FRONT+1)mod n==REAR
empty:(REAR+1)modn==FRONTempty:REAR==FRONT
(GATE 2012)
Answer:(A)
Explanation:Thecounterexamplefortheconditionfull:REAR=FRONTis
InitiallywhentheQueueisemptyREAR=FRONT=0bywhichtheabovefullconditionissatisfiedwhi
chisfalse
Thecounterexamplefortheconditionfull:(FRONT+1)modn=REARis
InitiallywhentheQueueisemptyREAR=FRONT=0andletn=3,soafterinsertingoneelementREAR
=1andFRONT=0,atthispointtheconditionfullaboveissatisfied,butstillthereisplacefor
onemoreelementinQueue,sothisconditionisalsofalse
Thecounterexamplefortheconditionempty:(REAR+1)modn=FRONTis
InitiallywhentheQueueisemptyREAR=FRONT=0andletn=2,soafterinsertingoneelementREAR
=1andFRONT=0,atthispointtheconditionemptyaboveissatisfied,butthequeueofcapacityn1isfullhere
Thecounterexamplefortheconditionempty:(FRONT+1)modn=REARis
InitiallywhentheQueueisemptyREAR=FRONT=0andletn=2,soafterinsertingoneelementREAR
=1andFRONT=0,atthispointtheconditionemptyaboveissatisfied,butthequeueofcapacityn1isfullhere
IT DEPARTMENT
GATE MATERIAL
(2012)
int a, b, c = 0;
voidprtFun (void);
int main ()
{
staticint a = 1; /* line 1 */
prtFun();
a += 1;
prtFun();
printf ( "\n %d %d " , a, b) ;
}
voidprtFun (void)
{
staticint a = 2; /* line 2 */
int b = 1;
a += ++b;
printf (" \n %d %d " , a, b);
}
50. What output will be generated by the given code segment?
(A) 3 1
(B) 4 2
(c) 4 2
(D) 3 1
41
61
62
52
42
61
20
52
Answer (C)
Explanation:
a and b are global variable. prtFun() also has a and b as local variables. The local variables hide the
globals (See Scope rules in C). When prtFun() is called first time, the local b becomes 2 and local a becomes
4.
When prtFun() is called second time, same instance of local static a is used and a new instance of b is
created because a is static and b is non-static. So b becomes 2 again and a becomes 6.
main() also has its own local static variable named a that hides the global a in main. The printf()
statement in main() accesses the local a and prints its value. The same printf() statement accesses the global
b as there is no local variable named b in main. Also, the defaut value of static and global int variables is 0.
That is why the printf statement in main() prints 0 as value of b.
51. What output will be generated by the given code d\segment if:
Line 1 is replaced by auto int a = 1;
Line 2 is replaced by register int a = 2;
(A) 3 1
(B) 4 2
(C) 4 2 (D) 4 2
41
61
62
42
42
61
20
20
(GATE 2012)
Answer (D)
Explanation:
IT DEPARTMENT
GATE MATERIAL
If we replace line 1 by auto int a = 1; and line 2 by register int a = 2;, then a becomes non-static in
prtFun(). The output of first prtFun() remains same. But, the output of second prtFun() call is changed as a
new instance of a is created in second call. So 4 2 is printed again. Finally, the printf() in main will print 2
0. Making a a register variable wont change anything in output.
IT DEPARTMENT
GATE MATERIAL