CS Programming Data Structure MCQ
CS Programming Data Structure MCQ
YEAR 2001
YEAR 2002
return s;
}
For large values of y, the return value of the function f best approximates
(A) x y (B) e x
(C) In(1 + x) (D) x x
Q. 8 Let T (n) be the number of different binary search trees on n distinct elements.
n
Then T [n] = / T (k - 1) T (x), where x is
k=1
(A) n - k + 1 (B) n - k
(C) n - k - 1 (D) n - k - 2
Q. 9 Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an
initially empty binary search tree. The binary search tree uses the usual ordering
on natural numbers. What is the inorder transversal sequence of the resultant
tree ?
(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
Q. 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
GATE SOLVED PAPER - CS PROGRAMMING & DATA STRUCTURE
Q. 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) 3 Y + 2X
(C) n ( X + Y ) - X (D) Y + 2X
Q. 12 Consider the following 2-3-4 tree (i.e., B-tree with a minimum degree of two) in
which each data item is a letter. The usual alphabetical ordering of letters is used
in constructing the tree
a =b;
b =temp;
}
In the 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
Q. 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
Q. 20 The best data structure to check whether an arithmetic expression has balanced
parenthesis is a
(A) queue (B) stack
(C) tree (D) list
Q. 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;
n=n/10;
}
GATE SOLVED PAPER - CS PROGRAMMING & DATA STRUCTURE
.n
(A) rear node (B) front node
(C) not possible with a single pointer (D) node next to front
Q. 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
Q. 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 + < # c - < < e < f is
(A) abc #+def ^^ - (B) abc #+de^ f^
(C) ab + c#d-e^ f^ (D) -+a#bc^^ def
GATE SOLVED PAPER - CS PROGRAMMING & DATA STRUCTURE
Q. 28 What does the following algorithm approximate ? (Assume m > 1, !> 0).
x=m;
y=1;
while (x-y>!)
{ x=(x+y)/2;
y=m/x;
}
print (x);
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.
Q. 30 Choose the best matching between the programming styles in Group 1 and their
characteristics in Group 2.
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 evaluation
S. Imperative 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
Q. 35 A program P reads in 500 integers in the range (0, 100) representing the scores
of 500 students. It then prints the frequency of each score above 50. What be the
best way for P to store the frequencies?
(A) An array of 50 numbers
(B) An array of 100 numbers
(C) An array of 500 numbers
(D) A dynamically allocated array of 550 numbers
Q. 41 Consider these two functions and two statements S1 and S2 about them.
int work1(int *a,int int work2(int *a,int
i,int j) i,int j)
{ {
int x=a int t1=i+2;
x=a[i+2]; int
return a t2=a[t1];
[i+2]-3;} 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
Q. 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
S 4 : 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 S4 (D) S2 and S 5
Q. 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
Q. 46 The following postfix expression with single digit operands in evaluated using a
stack
<2< / <2< *< <1 *<
Note that ^is the exponentiation operator. The top two elements of the stack after
the first* is evaluated are
(A) 6, 1 (B) 5, 7
(C) 3, 2 (D) 1, 5
Q. 48 Consider the following C program segment where Cell Node represents a node in
a binary tree
struct CellNode {
struct CellNode*leftchild;
int element;
struct CellNode*rightchild;
};
int GetValue(struct CellNode * ptr) {
int vlaue = 0;
if (ptr!=NULL) {
if ((ptr->leftChild = = NULL)&&
(ptr->rightChild = = NULL))
Value = 1;
else
value = value + GetValue
(ptr->leftChild)
+ Get Value
(ptr->rightChild);
}
return(value);
}
The value returned by Get Value when a pointer to the root of a binary tree is
passed as its argument is
(A) the number of nodes
(B) the number of internal nodes in the tree
(C) the number of leaf nodes in the tree
(D) the height of the tree
Q. 49 Which combination of the integer variables x, y, and z makes the variable a get
the value 4 in the following expression?
a = (x > y)? (( x > z)? x: z): (( y > z) ? y: z)
(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
Q. 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 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”);
}
(A) ?1 is (getchar () ! = ' \ n')
?2 is getchar (c);
(B) ?1 is (getchar ()) ! = ' \ n')
?2 is getchar (c);
(C) ?1 is (c ! = ' \ n')
?2 is putchar (c);
(D) ?1 is (( c = getchar ()) ! = ' \ n')
?2 is putchar (c);
Q. 54 What is the maximum height of any AVL-tree with 7 nodes ? Assume that the
height of a tree with a single node is 0.
(A) 2 (B) 3
(C) 4 (D) 5
Q. 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}
GATE SOLVED PAPER - CS PROGRAMMING & DATA STRUCTURE
Q. 57 The cyclomatic complexity of each of the modules A and B shown below is 10.
What is the cyclomatic complexity of the sequential integration shown on the
right hand side ?
(A) 19 (B) 21
(C) 20 (D) 10
Q. 59 What is the appropriate paring of items in the two columns listing various
activities encountered in a software life cycle ?
P. Requirement Capture 1. Module Development and Integration
Q. Design 2. Domain Analysis
R. Implementation 3. Structural and Behavioral Modeling
S. Maintenance 4. Performance Tuning
(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
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;
Q. 62 The following program is to be tested for statement coverage :
begin
if(a==b){S1;exit}
else if(c==d){S2;}
else {S3;exit;}
S4;
end
The test cases T1, T2, T3, and T4 given below are expressed in terms of the
properties satisfied by the values of variables a, b, c and d. The exact values
are not given.
T1 : a,b,c and d are all equal
T2 : a,b,c and d are all distinct
T3 : a=b and c!=d
T4 : a!=b and c=d
GATE SOLVED PAPER - CS PROGRAMMING & DATA STRUCTURE
Which of the test suites given below ensures coverage of statements S1, S2, S3
and S4 ?
(A) T1, T2, T3
(B) T2, T4
(C) T3, T4
(D) T1, T2, T4
0
1
2 42
3 23
4 34
5 52
6 46
7 33
8
9
Q. 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
Q. 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
Q. 66 A max-heap is a heap where the value of each parent is greater than or equal to
the value of its children. Which of the following is a max-heap?
where c is a constant
Q2: p A ... A ^s
1 p ^r hh
c1# A p #C 2
Q. 68 What is the return value of the function foo when it is called as foo(345, 10)
?
(A) 345 (B) 12
(C) 5 (D) 3
Q. 69 What is the return value of the function foo when it is called as foo(513, 2)?
(A) 9 (B) 8
(C) 5 (D) 2
Q. 71 The recurrence relation capturing the optimal execution time of the Towers of
Hanoi problem with n discs is
(A) T ^n h = 2T ^n - 2h + 2 (B) T ^n h = 2T ^n - 1h + n
(C) T ^n h = 2T ^n / 2h + 1 (D) T ^n h = 2T ^n - 1h + 1
Consider the calling chain: Main " A1 " A2 " A21 " A 1
The correct set of activation records along with their access links is given by
Q. 74 The height of a tree is defined as the number of edges on the longest path in the
tree. The function shown in the pseudocode below is invoked as the height (root)
to compute the height of a binary tree rooted at the tree pointer root.
int height (treeptr n)
{if (n==NULL) return 1;
GATE SOLVED PAPER - CS PROGRAMMING & DATA STRUCTURE
**********
GATE SOLVED PAPER - CS PROGRAMMING & DATA STRUCTURE
ANSWER KEY
Programming & Data Structure
1 2 3 4 5 6 7 8 9 10
(A) (C) (D) (B) (B) (B) (A) (A) (C) (B)
11 12 13 14 15 16 17 18 19 20
(D) (C) (D) (A) (B) (C) (D) (D) (B) (B)
21 22 23 24 25 26 27 28 29 30
(C) (A) (D) (C) (A) (A) (C) (C) (D) (D)
31 32 33 34 35 36 37 38 39 40
(C) (C) (C) (B) (A) (D) (C) (A) (A) (C)
41 42 43 44 45 46 47 48 49 50
(D) (C) (D) (A) (D) (A) (D) (C) (A) (B)
51 52 53 54 55 56 57 58 59 60
(D) (B) (B) (A) (C) (D) (D) (A) (B) (C)
61 62 63 64 65 66 67 68 69 70
(D) (D) (C) (C) (C) (B) (C) (B) (D) (C)
71 72 73 74 75 76
(D) (A) (A) (A) (C) (D)