Programming and Data Structure: GATE CS Topic Wise Questions
Programming and Data Structure: GATE CS Topic Wise Questions
Programming and Data Structure: GATE CS Topic Wise Questions
com
YEAR 2001
Question. 1
1( )
r x;
r y;
}
func1(x,y,z)
{
y=y+4
z=x+y+z;
}
(A) 10, 3 (B) 31, 3
(C) 27, 7 (D) None of the above
SOLUTION
Since the function fun 1 doesn’t return the values of x & y and x
& y are not passed by reference. So in program P1( ) would print
x = 10 & y = 3 . So 10, 3
Hence (A) is correct option.
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
Question. 2
Consider the following three functions :
[P1] int *g(void)
{
intx=10;
return (& x);
}
[P2] int *g(void)
{
int *px;
*px=10;
return px;
}
[P3] int *g(void)
{
int *px
px=(int*)malloc (size of (int));
*px=10;
return px;
}
Which of the above three functions are likely to cause problems with
pointers ?
(A) Only P3 (B) Only P1 and P3
(C) Only P1 and P2 (D) P1, P2 and P3
SOLUTION
Question. 3
Consider the following program
Program P2
Page 2
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
Var n:int:
procedure W (var x:int)
begin
X=X+1
Print x;
end
Procedure D
Begin
var n:int;
n=3;
W(n);
End
Begin \\begin P2
n=10;
D;
end
If the language has dynamic scooping and parameters are passed by
reference, what will be printed by the program ?
(A) 10 (B) 11
(C) 3 (D) None of the above
SOLUTION
YEAR 2002
Question. 4
SOLUTION
Question. 5
Consider the following declaration of a two-dimensional array in C :
Char a[100][100]
Assuming that the main memory is byte-addressable and that array
is stored starting form memory address 0, the address of a [40] [50] is
(A) 4040 (B) 4050
(C) 5040 (D) 5050
SOLUTION
YEAR 2003
Question. 6
Consider the following C function.
float f(float x, int y){
float p, s; int i;
for (s=1, p=1, i=1, i<y; i++)
Page 4
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
{
p)=x/i;
s+=p;
}
return s;
}
For large values of y, the return value of the function f best
approximates
(A) xy (B) ex
(C) In(1 + x) (D) xx
SOLUTION
Question. 7
Assume the following C variable declaration
int)A[10], B[10][10];
Page 5
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
Which will not give compile-time errors if used as left hand sides of
assignment statements in a C program ?
(A) 1, 2, and 4, only (B) 2, 3, and 4, only
(C) 2 and 4 only (D) 4 only
SOLUTION
Question. 8
(A) n − k + 1 (B) n − k
(C) n − k − 1 (D) n − k − 2
Page 6
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
SOLUTION
Binary search tree has a root node & its 2 subtrees. So for every node
other than the leaves, all the elements smaller than the node are its
left subtree & all the nodes which have value equal to or greater than
that node are at right subtree.
Figure
n (B) = no. of nodes in left subtree
n (C) " no. of nodes in right subtree
T (n) = n (B) + n (C) + 1
n
T (n) = /T (X) T (k − 1)
K=1
Question. 9
(A) 7 5 1 0 3 2 4 6 8 9 (B) 0 2 4 3 1 6 5 9 8 7
(C) 0 1 2 3 4 5 6 7 8 9 (D) 9 8 6 4 2 3 0 1 5 7
SOLUTION
Page 8
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
Question. 10
A data structure is required for storing a set of integers such that
each of the following operations can be done is (logn ) time, where n
is the number of elements in the set.
1. Delection of the smallest element.
2. Insertion of an element if it is not already present in the set.
Which of the following data structures can be used for this purpose ?
(A) A heap can be used but not a balanced binary search tree
(B) A balanced binary search tree can be used but not a heap
(C) Both balanced binary search tree and heap can be used
(D) Neither balanced binary search tree nor heap can be used
SOLUTION
Both the tasks can be performed by both the data structures but heap
Page 9
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
x = log n
Hence (B) is correct option.
Question. 11
Let S be a stack of size n $ 1. Starting with the empty stack, suppose
we push the first n natural numbers in sequence, and then perform n
pop operations. Assume that Push and Pop operation take X seconds
each , and Y seconds elapse between the end of the one such stack
operation and the start of the next operation. For m $ 1, define
the stack-life of mcs the time elapsed from the end or Push (m) to
the start of the pop operation that removes m from S . The average
stack-life of an element of this stack is
(A) n (X + Y) (B) 3Y + 2X
(C) n (X + Y) − X (D) Y + 2X
SOLUTION
Here each of PURSH & POP operation take X seconds & Y seconds
are elapsed between two consecutive stack operations.
m is the life time of element in stack.
So m X is time for push.
m X is time for pop.
m Y is time for intermediate
So total m (2X + Y)
m (2X + Y)
Average stack life =
m
= 2X + Y
= Y + 2X
Hence (D) is correct option.
Question. 12
Consider the following 2-3-4 tree (i.e., B-tree with a minimum degree
Page 10
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
SOLUTION
2-3-4 B-tree means the min degree of a node is two & it can be max
4 So maximum of 3 elements can be there in a node.
Question. 13
In the following C program fragment, j, k, n and TwoLog_n
are integer variables, and A is an array of integers. The variable n
is initialized to an integer $ 3 , and TwoLog_n is initialized to the
value of 2)6log 2 (n)@
for (k=3;k<=n;k++)
A[k]=0;
for (k=2;k<=TwoLog_n;k++)
for (j=k+1;j<=n;j++)
A[j]=A[j]<(j%k);
for(j=3;j<=n;j++)
if (!A[j])printf(“%d”,j);
The set of number printed by this program fragment is
(A) {m | m # n, (7i) [m = i!]} (B) {m | m # n, (7i) [m = i2]}
(C) {m | m # n, m is prime} (D) {m | m # n, m is odd}
SOLUTION
Question. 14
Consider the C program shown below.
#include <stdio.h>
#define print(x)printf(“%d”,x)
int x;
void Q (int z){
z+=x; print(z);
}
void p (int)y){
int x=)y+2;
Page 12
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
Q(x);)y=x-1;
print(x);
}
main (void){
x=5;
p(&x);
print(x);
}
The output of this program is
(A) 12 7 6 (B) 22 12 11
(C) 14 6 6 (D) 7 6 6
SOLUTION
Figure
Here X is the global variable so still 5.
Figure
First x=5
Then by function p(&x)
X =5+2=7
Then by function Q(x)
z =z+x
=7+5=12
Here x is global variable so still it is 5.
Return to function p(&x)
Y =7-1=6
print x =7
return to main
Print x =6
Here this is global x whose *y ahs been changed to
6 so 6 is printed.
Page 13
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
Question. 15
Consider the function - defined below.
struct item {
int data;
struct item)next;
};
int f (struct item )p){
return ((p==NULL)||(p− >next==NULL)||
((p− >data<=p− >next− >data)&&
f(p− >next)));
}
For a given linked list p, the function f return 1 if and only if
(A) the list is empty or has exactly one element
(B) the elements in the list are sorted in non-decreasing order of data
value
(C) the elements in the list are sorted in non-increasing order of data
value
(D) not all elements in the list have the same data value
SOLUTION
YEAR 2004
Question. 16
The goal of structured programming is to
(A) have well indented programs
(B) be able to infer the flow of control from the compiled code
(C) be able to infer the flow of control form the program text
(D) avoid the use of GOTO statements
Page 14
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
SOLUTION
Question. 17
SOLUTION
Question. 18
SOLUTION
Here the stack will be fuel if both top 1 & top 2 are at the adjacent
index values i.e. their difference is 1.
So top 1 = top 2 - 1
Here (D) is correct option.
Question. 19
The following numbers are inserted into an empty binary search tree
in the given order: 10, 1, 3, 5, 15, 12, 16. What is the height of the
binary search tree (tree height is the maximum distance of a leaf node
from the root) ?
(A) 2 (B) 3
(C) 4 (D) 6
SOLUTION
Question. 20
SOLUTION
Question. 21
Consider the following C function
int f(int n)
{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
SOLUTION
Question. 22
Consider the following program fragment for reversing the digits in a
given integer to obtain a new integer. Let n = d1 d2 .......dm.
int n, rev;
rev=0;
while(n>0){
rev=rev)10+n%10;
Page 18
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
n=n/10;
}
The loop invariant condition at the end of the ith iteration is
(A) n = d1 d2 ......dm − i and rev = dm dm − 1 ......dm − i + 1
(B) n = dm − i + 1 .....dm − 1 dm or rev = dm − i .....d2 d1
(C) n =
Y rev
(D) n = d1 d2 ....dm or rev = dm ......d2 d1
SOLUTION
Here after every iteration one digit is reduced from n since n = n/10
so unit place is removed.
This unit place is then added into the previous reverse sum (rev)
after multiplying rev by 10. So 1 digit is incremented every iteration.
So at the ith iteration n should have m − i digits d1 d2 .....dm − i & rev
have dm dm − 1 ..........dm − i + 1
i n rev
1 d1 d2 ....dm − 1 dm
2 d1 d2 ......dm − 2 dm dm − 1
So on.
Hence (A) is correct option.
Question. 23
Consider the following C program segment:
char p[20];
char)s= “string”;
int length=strlen(s);
for (i=0;i<length; i++)
p[i]=s[length i];
printf(“% s”, p);
The output of the program is
(A) gnirts (B) string
(C) gnirt (D) no output is printed
SOLUTION
In line 8 p[i]=S[length-i];
Here p is a character pointer variable so we can’t assign the value of
a pointer variable into character variable so no output is printed.
Page 19
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
Question. 24
SOLUTION
Here due to circular connection the rear & front are connected. Here
if we point P to rear the P "next point to front node & P " data
will point to rear value while inserting at rear following sequence of
operations done.
P " data = inserted value
)
P " next = P " next " next
These operation done is 0 (1) time
So constant complexity.
Hence (A) is correct option.
Page 20
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
Question. 25
The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the
given order into a maxHeap. The resultant maxHeap is
SOLUTION
Here n = 7 .
This is the heap of elements.
Now for max heap property is that every root node should be larger
than its child nodes. The root node on the tope is largest of all.
Step 1 take ^n/2h node for start ^7/2h = 4 if it is at right place i.e it is
smaller than its parent & greater than its children then OK otherwise
swap them.
Question. 26
Assume that the operators + , - , # are left associative and ^ is
right associative .The order of precedence (from highest to lowest)
is ^, # , + , -. The postfix expression corresponding to the infix
expression a + b # c - d ^ e ^ f is
(A) abc #+def ^^− (B) abc #+de^f^
(C) ab+c#d−e^f^ (D) −+a#bc^^def
SOLUTION
Given expression a + b ) c − d / e / f
parenthesizing the expression as per given rules.
= ((a + (b ) c)) − (d / (e / f)))
= ((a + (bc ))) − (d / (ef /)))
= ((abc ) +) − (def //))
= (abc ) + def //−)
So option (A) is correct
You can also solve using stack.
Hence (A) is correct option.
Question. 27
Consider the following C program
main ()
{ int x, y, m, n;
scanf(“%d%d”, &x,&y);
/)Assume 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 computers
(A) x ' y, using repeated subtraction
(B) x mod y using repeated subtraction
(C) the greatest common divisor of x and y
Page 22
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
SOLUTION
Question. 28
SOLUTION
Iteration X Y
(16 + 1) 16 = 2
1 =8
2 8
Page 23
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
2 8+2 = 5 16 = 3
2 5
3 5+3 = 4 16 = 4
2 4
Here X = Y
Then take X . which is 4.
(m) 1/2 = 4 = (16) 1/2
Hence (C) is correct option.
Question. 29
int element;
struct CellNode )rightChild;
}
int DoSomething (struct CellNode )ptr)
{
int value=0;
if(ptr!=NULL)
{ if (ptr− >leftChild !=NULL)
value=1+DoSomething (ptr− >leftChild);
if (ptr− >rightChild!=NULL)
value=max(value,1+DoSomething(ptr − >
right child));
return (value);
}
The value returned by the function DoSomething when a pointer to
the proof of a non-empty tree is passed as argument is
(A) The number of leaf nodes in the tree
(B) The number of nodes in the tree
(C) The number of internal nodes in the tree
(D) The height of the tree.
SOLUTION
Value initialized by 0
If any root node has left child then it adds 1 to the value & move to
Page 24
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
left child & if any mode has right child also then also calculated the
value using recursion & take maximum of both left & right value is
taken.
So we know that height is the largest distance between root node &
leaf.
So this program calculates heights.
Hence (D) is correct option.
Question. 30
Group-I Group-2
P. Functional 1. Command-based, procedural
Q. Logic 2. Imperative, abstract data types
R. Object-oriented 3. Side-effect free, declarative, expression
S. Imperative evaluation
4. Declarative, clausal representation,
theorem proving
(A) P-2, Q-3, R-4, S-1 (B) P-4, Q-3, R-2, S-1
(C) P-3, Q-4, R-1, S-2 (D) P-3, Q-4, R-2, S-1
SOLUTION
YEAR 2005
Question. 31
SOLUTION
Question. 32
SOLUTION
Question. 33
SOLUTION
Question. 34
SOLUTION
Question. 35
SOLUTION
Here the no. readable are range 0 to 100 but the output of the program
is interested in scores above 50 so there are 50 values (51 to 100) in
this range.
So only an array 50 integers required as we get any no. we increment
the value stored at index.
Array [x − 50] by 1.
Hence ( ) is correct option.
Question. 36
SOLUTION
Question. 37
SOLUTION
Question. 38
Postorder traversal of a given binary search tree, T produces the
following sequence of keys
10, 9, 23, 22, 27, 25, 15, 50, 95, 60, 40, 29
Which one of the following sequences of keys can be the result of an
inorder traversal of the tree T?
(A) 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
(B) 9, 10, 15, 22, 40, 50, 60, 95, 23, 25, 27, 29
(C) 29, 15, 9, 10, 25, 22, 23, 27, 40, 60, 50, 95
(D) 95, 50, 60, 40, 27, 23, 22, 25, 10, 0, 15, 29
SOLUTION
When we are given any no elements & even any order (preorder or
post order) & we need to calculate inorder, then inorder is simply
sorted sequence of the elements.
Here 9, 10, 15, 22, 23, 25, 27, 29, 40, 50, 60, 95
Hence (A) is correct option.
YEAR 2006
Question. 39
An implementation of a queue Q, using two stacks S1 and S2 , is
given below
void insert(Q,x){
push (S1,x);
}
void delete(Q, x){
if (stack-empty (S2))then
Page 30
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
if (stack-empty (S1))then{
print(“Q is empty”);
return;
}
else while (! (stack-empty)(S1)){
x=pop(S1);
push(S2,x);
}
x=pop(S2);
}
Let n insert and m (# n) delete operations be performed in an
arbitrary on an empty queue Q, Let x and y be the number of push
and pop operations performed respectively in the processes. Which
one of the following is true for all m and n ?
(A) n + m # x < 2n and 2m # n + m
(B) n + m # x < 2n and 2m # y # 2n
(C) 2m # x < 2n and 2m # y # n + m
(D) 2m # x < 2n and 2m # y # 2n
SOLUTION
Question. 40
Consider the following C-function in which a[n] and b[n] are two
sorted integer arrays and c[n+m] be another integer array.
void xyz (int a[],int b[],int c[]){
int i, j, k;
i=j=k=0;
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+j-1, and b[m-1]#a[i] if j=m
(A) only (I)
(B) only (II)
Page 31
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
SOLUTION
Question. 41
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 a i int t1=i+2;
a j 1 int t2=a[t1];
return a i a[j]=t2+1
} return t2-3;
}
S1: The transformation from work 1 to work 2 is valid, i.e., for any
program state and input arguments, work 2 will compute the
same output and have the same effect on program state as work
1
S2: All the transformations applied to work 1 to get work 2 will
always improve the performance (i.e. reduce CPU time) of work
2 compared to work 1
(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
SOLUTION
Question. 42
Consider the C code to swap two integers and these five statements:
the code
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
referreing to integers stored in memory locations accessible tot
he 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 S 3
(C) S2 and S 4 (D) S2 and S5
SOLUTION
Here pointers are used without initialization also the address pointed
by then may be out of segment of program, so segmentation.
" Fault may be there so. S2 correct.
" Here no compiler error S1 false.
" Correctly done swap procedure but not all valid import pointers
so S 4 also true.
S2 & S 4 are correct.
Hence (C) is correct option.
Page 33
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
Question. 43
Which one of the following is a valid sequence of elements in an array
representing 2-ary max heap ?
(A) 1, 3, 5, 6, 8, 9 (B) 9, 6, 3, 1, 8, 5
(C) 9, 3, 6, 8, 5, 1 (D) 9, 5, 6, 8, 3, 1
SOLUTION
Here in option (A), (B) and (C), value present at node is not greater
Page 34
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
Question. 44
Suppose the elements 7, 2, 10, and 4 are inserted, in that order, into
the valid 3-ary max heap found in the above question, Q. 33. Which
on of the following is the sequence of items in the array representing
the resultant heap ?
(A) 10, 7, 9, 8, 3, 1, 5, 2, 6, 4
(B) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
(C) 10, 9, 4, 5, 7, 6, 8, 2, 1, 3
(D) 10, 8, 6, 9, 7, 2, 3, 4, 1, 5
SOLUTION
Compare elements with its parent node. Since 10 > 6 and 7 >5, we
interchange
Page 35
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
n = 10 = 5
2 2
3 is at right position
4 is at right position
Figure Figure
Figure
Order
10 7 9 8 3 1 5 2 6 4
YEAR 2007
Question. 45
Consider the following segment of C-code
int, , n
while n
Page 36
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
SOLUTION
Iteration j Comparison
Initially 0 1=2 0
1 2 = 21
2 4 = 22
3 8 = 23
Here condition is 2i < =n
In i iteration & it requires i + 1 comparisons.
th
i = log 2 n
i + 1 = ^log 2 nh + 1
Hence (A) is correct option.
Question. 46
SOLUTION
Stack contain 5, 7
Hence (B) is correct option.
Question. 47
SOLUTION
Given f (5) = ?
Recursion n r Return Final Return
1 5 0"5 f (3) + 2 16 + 2 = 18
Page 38
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
Question. 48
SOLUTION
Here the structure cell Node represents a binary tree. Here the
function Get value return 1 if for any node both left & right children
are NULL or we can say if that node is a leaf node.
A variable value initialized to 0 is there to count these leaf nodes.
Hence (C) is correct option
YEAR 2008
Question. 49
(A) x = 3, y = 4, z = 2 (B) x = 6, y = 5, z = 3
(C) x = 6, y = 3, z = 5 (D) x = 5, y = 4, z = 5
SOLUTION
Question. 50
What is printed by the following C program?
int f(int x, int *py, int **ppz) void main()
{ {
int y, z int , * , ** a
** ppz z * ppz a
*py y * py printf (" d", f ( , , a))
x
return x y z
(A) 18 (B) 19
(C) 21 (D) 22
SOLUTION
Let c=4c =4
b = & c = 1000
a & b = 2000
Call f (4, 1000, 2000)
ppz = 2000 py = 1000 X = 4
Question. 51
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
Page 41
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
SOLUTION
Here if and if the string comes we print the letter & do it recursively.
If C is not end to string then we move to next character in the string.
?1 should be to getchar in C & check if it is end of string.
Hence (C=getchar ()!= ‘\n’ )
?2 should be when‘\n’ reached so print. putchar (C);
Hence (D) is correct option.
Question. 52
SOLUTION
Here the code scans the whole list & exchanges two consecutive
elements which are the nodes p & q. and then move to next two
elements or 2 * 1 4 * 6 * 5 7
Thus 2 1 4 3 6 5 7 is the output.
Hence (B) is correct option.
YEAR 2009
Question. 53
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;
Page 43
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
printf(“%d\n”,fun(5,&x));
return 0;
}
The value printed is:
(A) 6 (B) 8
(C) 14 (D) 15
SOLUTION
Here the table column I, II, & III are during the forward calls of the
recursive function fun &
The part after arrow in column III is updated value during return
calls & column IV & V are the returned values.
In the end 8 is returned so only this will be printed
Hence (B) is correct option.
Question. 54
SOLUTION
AVL tree is a partially balanced tree with the weights assigned to the
nodes can be only − 1, 0 or 1. This weight is assigned on the basis of
difference of the no. of children in the left subtree & right subtree. If
some other weight is there then we rotate the tree to balance it.
Page 44
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
This is balanced.
Question. 55
Which one of the follow9ng array represents a binary max-heap?
(A) {25, 12, 16, 13, 10, 8, 14}
(B) {25, 14, 13, 16, 10, 8, 12}
(C) {25, 14, 16, 13, 10, 8, 12}
(D) {25, 14, 12, 13, 10, 8, 16}
SOLUTION
If the value presented at any node is greater then all its children then
the tree is called the max heap.
Page 45
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
Question. 56
What is the content of the array after two delete operations on the
correct answer to the previous question?
(A) {14, 13, 12, 10, 8} (B) {14, 12, 13, 8, 10}
(C) {14, 13, 8, 12, 10} (D) {14, 13, 12, 8, 10}
SOLUTION
Page 46
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
YEAR 2010
Question. 57
(A) 19 (B) 21
(C) 20 (D) 10
SOLUTION
Question. 58
SOLUTION
Question. 59
(A) P-3 Q-2, R-4 S-1 (B) P-2 Q-3 R-1 S-4
(C) P-3 Q-2 R-1 S-4 (D) P-2 Q-3 R-4 S-1
SOLUTION
All of these steps are part of a simple software development life cycle
(SWDLC)
P. Requirement Capture : Considered as first step where we analyze
the problem scenario, domain of input, range of output and
effects.
P Design : Knowing the problem a systematic structure of the
problem solution is designed and the behavior modelling refers to
the functions of the module, which give certain output providing
definite inputs.
R Implementation : After knowing behavior the modules are
developed, converting the logics in the programming logics. The
independent modules are then integrated.
S Maintenance : Successful implementation done but even then
the performance might not optimal so some features or methods
need to be change to tune the performance.
Hence (B) is correct option.
Question. 60
SOLUTION
Question. 61
The following C function takes a singly-linked list as input argument.
It modified the list by moving the last element to the front of the list
and returns the modified list. Some part of the code is left blank.
typedef struct node {
int value;
struct node *next
} Node;
Node )mode_to_front(Node *head){
Node)p,) ;
if((head==NULL)<(head->next==NULL))return head;
q=NULL;p=head;
Page 50
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
while(p->next!=NULL){
q=p;
p=q->next;
}
________________________
return head;
}
Choose the correct alternative to replace the blank line.
(A) q=NULL;p->next=head;head=p;
(B) q->next=NULL;head=p;p->next=head;
(C) head=p;p->next=q;q->next=NULL;
(D) q->next=NULL;p-next=head;head=p;
SOLUTION
Here the program wants to make the last node of the list, the first
node.
Here q is the second last node and p is the last node
" The second last node’s next should be now NULL so
q->next=NULL.
" p->next should below head node.
so p->next=head
" Now the head node is p.
So head = p.
Hence (D) is correct option.
Question. 62
SOLUTION
0
1
2 42
3 23
4 34
5 52
6 46
7 33
Page 52
CS Topicwise 2001-2010
www.gatehelp.com Programming & Data
Structure
8
9
Question. 63
Which one oft he following choices gives a possible order in which the
key values could have been inserted in the table ?
(A) 46, 42, 34, 52, 23, 33 (B) 34, 42, 23, 52, 33, 46
(C) 46, 34, 42, 23, 52, 33 (D) 42, 46, 33, 23, 34, 52
SOLUTION
Here for hashing Linear probing is used, i.e. it finds the hash key
value through hash function and maps the key on particular position
In Hash table. In case of key has same hash address then it will find
the next address then it will find the next empty position in the Has
Table.
Here we check all options:
(A) Here 42 will be inserted at the 2nd position in the array next 52,
also has same hash address 2. But it already occupied so it will search
for the next free place which is 3rd position. So here 52 is misplaced
and it is not possible key values.
Table 3 table
(B) Here 46 is misplaced so it is not possible value.
(C) This is same as given hash table.
So correct order is 46, 34, 42, 23, 52, 33
Hence (C) is correct option.
Question. 64
How many different insertion sequences of the key values using hte
same hash function and linear probing will result in the hash table
shown above ?
(A) 10 (B) 20
(C) 30 (D) 40
SOLUTION
Here the given order of insertion is 46, 34, 42, 23, 52, 33
Page 53
CS Topicwise 2001-2010
Programming & Data www.gatehelp.com
Structure
Figure
" Here 42, 23, 34, 46 are inserted direct using hash function
" But to insert 52 we have 6 vacant places.
" After insertion of 52 at any of the 6 places, we have 5 places
remaining for 33.
So total combination.
6 # 5 = 30 possible ways
Hence (C) is correct option.
**********
Page 54
By NODIA and Company
Available in Two Volumes