0% found this document useful (0 votes)
11 views16 pages

DS CCW

The document contains a series of programming and data structure questions from GATE exams, along with their correct answers. Topics covered include binary search trees, time complexities, C programming, and data structures like stacks and queues. Each question is followed by multiple-choice options and the correct answer is provided for each.

Uploaded by

kpu21195
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views16 pages

DS CCW

The document contains a series of programming and data structure questions from GATE exams, along with their correct answers. Topics covered include binary search trees, time complexities, C programming, and data structures like stacks and queues. Each question is followed by multiple-choice options and the correct answer is provided for each.

Uploaded by

kpu21195
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

The preorder traversal of a binary search tree is 15, 10,


12, 11, 20, 18, 16, 19. Which one of the following is the
postorder traversal of the tree? [GATE CSE 2020]
(A) 10, 11, 12, 15, 16, 18, 19, 20
(B) 11, 12, 10, 16, 19, 18, 20, 15
(C) 20, 19, 18, 16, 15, 12, 11, 10
(D) 19, 16, 18, 20, 11, 12, 10, 15
Solution: Correct answer is (B)
2. What is the worst-case time complexity of inserting n
elements into an empty linked list, if the linked list needs to
be maintained in sorted order? More than one answer may
be correct. [GATE CSE 2020]
(A) Θ(n)
(B) Θ(n log n)
(C) Θ(n2)
(D) Θ(1)
Solution: Correct answer is (C)
3. What is the worst-case time complexity of inserting
n2 elements into an AVL tree with n elements initially?
[GATE CSE 2020]
(A) Θ(n4)
(B) Θ(n2)
(C) Θ(n2 log n)
(D) Θ(n3)
Solution: Correct answer is (C)
4. Consider the following C program. [GATE CSE 2018]
#include<stdio.h>
struct Ournode{
char x,y,z;
};
int main(){
struct Ournode p = {'1', '0', 'a'+2};
struct Ournode *q = &p;
printf ("%c, %c", *((char*)q+1), *((char*)q+2));
return 0;
}
The output of this program is:
(A) 0, c
(B) 0, a+2
(C) ‘0’, ‘a+2’
(D) ‘0’, ‘c’
Solution: Correct answer is (A)
5. Consider the following C Program [GATE CSE 2016]
void f(int, short);
void main()
{
int i = 100;
short s = 12;
short *p = &s;
___________ ; // call to f()
}
Which one of the following expressions, when placed in the
blank above, will NOT result in a type-checking error?
(A) f(s, *s)
(B) i = f(i,s)
(C) f(i, *s)
(D) f(i, *p)
Solution: Correct answer is (D)
6. A queue is implemented using an array such that
ENQUEUE and DEQUEUE operations are performed
efficiently. Which one of the following statements
is CORRECT (n refers to the number of items in the queue)?
[GATE CSE 2016]
(A) Both operations can be performed in O(1) time
(B) At most one operation can be performed on O(1) time but the
worst-case time for the other operation will be Ω(n)
(C) The worst-case time complexity for both operations will be Ω(n)
(D) Worst case time complexity for both operations will be Ω(log n)
Solution: Correct answer is (A)
7. The result evaluating the postfix expression 10 5 + 60 6/
* 8 – is [GATE CSE 2015]
(A) 284
(B) 213
(C) 142
(D) 71
Solution: Correct answer is (C)
8. Let A be a square matrix of size n x n. Consider the
following program. What is the expected output? [GATE CSE
2014]
C = 100
for i = 1 to n do
for j = 1 to n do
{
Temp = A[i][j] + C
A[i][j] = A[j][i]
A[j][i] = Temp - C
}
for i = 1 to n do
for j = 1 to n do
Output(A[i][j]);
(A) The matrix A itself
(B) Transpose of the matrix A
(C) Adding 100 to upper diagonal elements and subtracting 100
from diagonal elements of A
(D) None of the above
Solution: Correct answer is (A)
9. Consider the following program in C language: [GATE CSE
2014]
#include<stdio.h>
main()
{
int i;
int *pi = &i;
scanf("%d", pi);
printf("%d\n", i+5);
}
Which one of the following statements is TRUE?
(A) Compilation fails
(B) Execution results in a run-time error
(C) On execution, the value printed is 5 more than the address of
variable i
(D) On execution, the value printed is 5 more than the integer
value entered
Solution: Correct answer is (D)
10. A program P reads 500 integers in the range [0, 100]
representing the cores of 500 students. It then prints the
frequency of each score above 50. What would be the best
way for P to store the frequencies? [GATE CSE 2005]
(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
Solution: Correct answer is (A)

11. Which of these is an infix expression?

a. a+b*c

b. ab+c-

c. abc+*d-e+

d. a*+b(c-d)

Answer: (a) a+b*c

12. A stack implements _________:

a. FIFO principle

b. LIFO principle

c. Hierarchical structure

d. Disordered array

Answer: (b) LIFO principle

13. The time complexity for deleting a node in a linked list is:

a. O(1)

b. O(n)

c. O(nlogn)
d. O(logn)

Answer: (b) O(n)

14. How many of possible ordered trees with 3 nodes A, B, C will be formed ?
(a) 16 (b) 12
(c) 6 (d) 10

Solution: Option (b)


Explanation:
Tree maybe of depth 2 or 1. If depth is 2, we have 6 possible trees. This is because
one of the 3 nodes A, B, C may be the root and the next level may be one of the
remaining 2 nodes.

If the depth is 1, the root maybe one of the 3 nodes A, B, C. Corresponding to a root,
say A, 2 trees are possible as this.

This gives us 6 more possibilities.

15. A binary tree in which every non-leaf node has non-empty left and right
subtrees is called a strictly binary tree. Such a tree with 10 leaves.
(a) maximum 19 nodes
(b) has exactly 19 nodes
(c) has exactly 17 nodes
(d) cannot have more than 17 nodes
Solution: Option (b)
Explanation:
The configuration of 10 leaves can only be of the following way:
Any tree with n-leaves, for strict binary tree has (2n-1) nodes.

1. A program P reads in 500 integers in the range [0, 100], representing the
cores of 500 students. It then prints the frequency of each score above 50.
What would be the best way for P to store the frequencies?

(GATE CSE 2005)

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

Answer (a)

2. A single array A[1..MAXSIZE] is used to implement two stacks. The two stacks
grow from opposite ends of the array. Variables top1 and top2 (top1 < top2)
point to the location of the topmost element in each of the stacks. If the space
is to be used efficiently, the condition for “stack full” is ___________.

(GATE CSE 2004)

a. (top 1 = MAXSIZE/2) AND (top 2 = MAXSIZE/2 + 1)


b. top 1 + top 2 = MAXSIZE
c. (top 1 = MAXSIZE/2) or (top 2 = MAXSIZE)
d. top 1 = top 2 – 1

Answer (d)

3. Suppose you are given an array s[1..n] and a procedure reverse (s, i, j) which
reverses the order of elements in s between positions i and j (both inclusive).
What does the following sequence do, where 1 ≤ k < n: reverse (s, 1, k);

reverse (s, k+1, k);


reverse (s, 1, n);

(GATE CSE 2000)

a. Rotates s left by k positions


b. Leaves s unchanged
c. Reverse all elements of s
d. None of the above

Answer (a)

4. Consider a double hashing scheme in which the primary hash function is


h1(k)=k mod 23, and the secondary hash function is h 2(k)=1+(k mod 19).
Assume that the table size is 23. Then the address returned by probe 1 in the
probe sequence (assume that the probe sequence begins at probe 0) for key-
value k=90 is ________.

(GATE CSE 2020)

a. 8
b. 13
c. 15
d. 20

Answer (b)

5. Consider the following two statements:

i. A hash function (these are often used for computing digital


signatures) is an injective function.

ii. encryption technique such as DES performs a permutation on the


elements of its input alphabet.

Which one of the following options is valid for the above two
statements?

(GATE CSE 2007)

a. Both are false


b. Statement i is true and statement ii is false
c. Statement ii is true and statement i is false
d. Both are true

Answer (c)

6. A hash table contains 10 buckets and uses linear probing to resolve collisions.
The key values are integers, and the hash function used is key % 10. If the
values 43, 165, 62, 123, 142 are inserted in the table, in what location would
the key value 142 be inserted?
(GATE CSE 2005)

a. 2
b. 3
c. 4
d. 6

Answer (d)

7. Given the following input (4322, 1334, 1471, 9679, 1989, 6171, 6173, 4199)
and the hash function x mod 10, which of the following statements are true?

i) 9679, 1989, 4199 hash to the same value

ii) 1471, 6171 has to the same value

iii) All elements hash to the same value

iv) Each element hashes to a different value

(GATE CSE 2004)

a. i only
b. ii only
c. i and ii only
d. iii or iv

Answer (c)

8. N items are stored in a sorted doubly linked list. For a delete operation, a
pointer is provided to the record to be deleted. For a decrease-key operation,
a pointer is provided to the record on which the operation is to be performed.

An algorithm performs the following operations on the list in this


order: Θ(N), delete, O(log⁡N) insert, O (log⁡N) fund, and Θ(N) decrease
key. What is the time complexity of all these operations put
together?

(GATE CSE 2016 Set 2)

a. O(log2N)
b. O(N)
c. O(N2)
d. Θ(N2logN)

Answer (c)

9. Let P be a singly linked list; let Q be the pointer to an intermediate node x in


the list. What is the worst-case time complexity of the best-known algorithm
to delete node x from the list?

(GATE CSE 2004)


a. O(n)
b. O(log2n)
c. O(log n)
d. O(1)

Answer (d)

10. A queue is implemented using an array such that ENQUEUE and DEQUEUE
operations are performed efficiently. Which one of the following statements
is CORRECT (n refers to the number of items in the queue)?

(GATE CSE 2016 Set 1)

a. Both operations can be performed in O(1) time


b. At most, one operation can be performed in O(1) time, but the worst-
case time for the other operation will be Ω(n)
c. The worst-case time complexity for both operations will ne Ω(n)
d. The worst-case time complexity for both operations will be Ω(log n)

Answer (a)

11. A function f defined on stacks of integers satisfies the following properties.


f(∅) = 0 and f (push (S, i)) = max (f(S), 0) + i for all stacks S and integers i. If a stack S contains
the integers 2, -3, 2, -1, 2 in order from bottom to top, what is f(S)?

(GATE CSE 2005)

a. 6
b. 4
c. 3
d. 2

Answer (c)

12. Consider the following statements:

(i) First-in-first out types of computations are efficiently supported


by STACKS.

(ii) Implementing LISTS on linked lists is more efficient than


implementing LISTS on an array for almost all the basic LIST
operations.

(iii) Implementing QUEUES on a circular array is more efficient than


implementing QUEUES on a linear array with two indices.

(iv) Last-in-first-out type of computations are efficiently supported


by QUEUES.

(GATE CSE 1996)


a. ii and iii are true
b. i and ii are true
c. iii and iv are true
d. ii and iv are true

Answer (a)

13. The postorder traversal of a binary tree is 8,9,6,7,4,5,2,3,1. The inorder


traversal of the same tree is 8,6,9,4,7,2,5,1,3. The height of a tree is the
length of the longest path from the root to any leaf. Then the height of the
binary tree is ________.

(GATE CSE 2018)

a. 5
b. 4
c. 10
d. 1

Answer (b)

14. A scheme for storing binary trees in an array X is as follows. Indexing of X


starts at 1 instead of 0. The root is stored at X[1]. For a node stored at X[i],
the left child, if any, is stored in X[2i] and the right child, if any, in X[2i+1]. To
be able to store any binary tree on n vertices, the minimum size of X should
be ________.

(GATE CSE 2006)

a. Log n
b. n
c. 2n + 1
d. 2n – 1

Answer (b)

15. 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 (the height is the maximum distance of a leaf node from the root)?

(GATE CSE 2004)

a. 2
b. 3
c. 4
d. 5

Answer (b)

1. A queue follows _________:

a. LIFO principle
b. FIFO principle

c. Linear tree

d. Ordered array

Answer: (b) FIFO principle

2. The time complexity used for inserting a node in a priority queue on the
basis of key is:

a. O(n)

b. O(n2)

c. O(nlogn)

d. O(logn)

Answer: (a) O(n)

3. Which of these is a postfix expression?

a. a+b-c

b. +ab

c. abc*+de-+

d. a*b(c+d)

Answer: (c) abc*+de-+

4. Which data structure do we use for testing a palindrome?

a. Heap

b. Tree

c. Priority queue

d. Stack

Answer: (d) Stack

5. Which of these will form an inversion in this given array?


arr = {2,8,5,3}

a. (2,8)

b. (8,5), (8,3)

c. (2,8), (2,5), (1,3)

d. (8,5), (8,3), (5,3)

Answer: (d) (8,5), (8,3), (5,3)

6. Which one isn’t the property of the XOR lists?

a. X⊕0 = X

b. X⊕X = 0

c. X⊕0 = 1

d. (X⊕Y)⊕Z = X⊕(Y⊕Z)

Answer: (c) X⊕0 = 1

7. The tango tree is a type of:

a. Binary Search Tree

b. K-ary Tree

c. Ternary Tree

d. AVL Tree

Answer: (a) Binary Search Tree

8. In an AA-tree, we can remove a left horizontal link by:

a. inserting a new element

b. deleting both the elements

c. performing left rotation

d. performing right rotation


Answer: (d) performing right rotation

9. We can use a self–balancing binary search tree for implementing the:

a. Hash table

b. Priority queue

c. Heap sort and Priority queue

d. Heap sort

Answer: (b) Priority Queue

10. A splay operation refers to:

a. the removal of leaf node

b. the movement of root to leaf

c. the movement of a node to root

d. the movement of parent node to a child node’s down

Answer: (c) the movement of a node to root

11. Out of these, which one is NOT true about a 2-3 tree?

a. it is perfectly balanced

b. the leaves are always at the same level

c. it refers to a B-tree of the order 3

d. postorder traversal would yield the elements in a sorted order

Answer: (d) postorder traversal would yield the elements in a sorted


order

12. How do we define the Ackermann’s function?

a. for i<1, A(1,i) = i+1

b. for i = j, A(i,j) = i+j

c. for i>=j, A(i,j) = i+j


d. for i>=1, A(1,i) = i+1

Answer: (d) for i>=1, A(1,i) = i+1

13. A recursive implementation would presumably fail in skew heaps


because:

a. lack of stack space

b. time complexity

c. these heaps are self adjusting

d. efficiency gets reduced

Answer: (a) lack of stack space

14. Which operation can we NOT perform directly in a d-heap?

a. create

b. find

c. delete

d. insert

Answer: (b) find

15. The time does taken for the construction of suffix tree is:

a. Linear to the Length of Tree

b. Exponential to the Length of Tree

c. O (M!)

d. O (log M)

Answer: (a) Linear to the Length of Tree

16. The best technique for handling collision is:

a. Separate chaining

b. Double hashing
c. Linear probing

d. Quadratic probing

Answer: (d) Quadratic probing

17. Which one is the most desirable out of these traits of a hash function?

a. it must cause more collisions

b. it must be easy to implement

c. it must cause less collisions

d. it must occupy less space

Answer: (c) it must cause less collisions

18. What is the time complexity for checking if an undirected graph with E
edges and V vertices is Bipartite, given its adjacency matrix?

a. O(E)

b. O(V)

c. O(E*E)

d. O(V*V)

Answer: (d) O(V*V)

19. The members of two of the sets are relatively more common when the
Jaccard Index is:

a. Closer to 0

b. Closer to 1

c. Farther to 1

d. Closer to -1

Answer: (b) Closer to 1


20. The polynomial-time graph manipulation algorithms can’t implement
which of these logical operations using the Binary Decision Diagrams?

a. Tautology Checking

b. Negation

c. Disjunction

d. Conjunction

Answer: (a) Tautology Checking

You might also like