Download MCQ`S For Data Structure and Algorithms 1. Suppose that we have

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Array data structure wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Linked list wikipedia , lookup

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

B-tree wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

Binary search tree wikipedia , lookup

Transcript
MCQ’S For Data Structure and Algorithms
1. Suppose that we have a data file containing records of famous people, and we need to build a
hash table to find a record from the person's birthday. The size of the hash table is 4096. The
following are hash functions which convert a birthday to an integer. Which of the following
function is the best?
(a)
(b)
(c)
(d)
(e)
h1( day/month/year ) = day + month + year
h2( day/month/year ) = day + month*31 + year
h3( day/month/year ) = (day + month*31 + year*365) mod 4096
h4( day/month/year ) = (day + month*31 + year*365) mod 4093
h5(day/month/year ) = (day + month*31 + year*365)
2. Analysis of Selection algorithm ends up with,
T(n)
T(1 / 1 + n)
T(n / 2)
T((n / 2) + n)
3. Merge sort makes two recursive calls. Which statement is true after these recursive calls
finish,
but
before
the
merge
step?
a)
The
array
elements
form
a
heap.
b)
None
of
the
above.
c)
Elements in each half of the array are sorted amongst themselves.
d)
Elements in the first half of the array are less than or equal to elements in the second half
of
the
array.
4The OBST algorithm in worst case takes _______ time if all c(i, j )’s and r(i, j)’s are
calculated.
(a) O(log n)
(b) O(n4)
a) Something else
(c) O(n3)
(d) O(n log n)
5. How much time merge sort takes for an array of numbers?
T(n^2)
T(n)
T( log n)
T(n log n)
(e) O(n2).
6. The recurrence relation of Tower of Hanoi is given below T(n)={1 if n=1 and 2T(n-1) if n
>1 In order to move a tower of 5 rings from one peg to another, how many ring moves are
required?
16
10
32
31
7. Slow sorting algorithms run in,
T(n^2)
T(n)
T( log n)
T(n)
8. Searching an element in an AVL tree take maximum in AVL tree,
a) Log2(n+1)
b) Log2(n+1) -1
c) 1.44 Log2n
d) 1.66 Log2n
9. Which sorting algorithn is faster :
O(n^2)
O(nlogn)
O(n+k)
O(n^3)
10. Which sorting algorithm is faster
O (n log n)
O n^2
O (n+k)
O n^3
11. When we say the order of a tree is M, we mean
(a)
(b)
(c)
(d)
(e)
Every non-leaf node must have M subtrees
Every non-leaf node must have M keys
Every non-leaf node can have at most M subtrees
Every non-leaf node can have at most M keys
The Height of the tree is M.
12. Heaps can be stored in arrays without using any pointers; this is due to the ____________
nature of the binary tree,
left-complete
right-complete
tree nodes
tree leaves
13. In a selectionsort of n elements, how many times is the swap function called in the complete
execution of the algorithm?
a)
1
b)
n-1
c)
n log n
d)
n²
14. Consider the following binary search tree (BST):
If node A in the BST is deleted, which two nodes are the candidates to take its place?
a) J and I
b) H and E
c) D and E
d) L and M
15. Suppose we are sorting an array of ten integers using a some quadratic sorting algorithm.
After four iterations of the algorithm's main loop, the array elements are ordered as shown here:
1 2 3 4 5 0 6 7 8 9
Which statement is correct? (Note: Our selection sort picks largest items first.)
a)
The algorithm might be either selection sort or insertion sort.
b)
The algorithm is neither selection sort nor insertion sort.
c)
The algorithm might be selection sort, but could not be insertion sort.
d)
The algorithm might be insertion sort, but could not be selection sort.
16. What is the running time of the following code fragment?
for(int i=0; i<10; i++)
for(int j=0; j<N; j++)
for(int k=N-2; k<N+2; k++)
cout << i << " " << j << endl;
a) O(log N)
b) O(N log N)
c) O(N)
d) O(N2)
17. Which of the following is NOT true regarding recursion and
iteration?
A. Any recursive method can be rewritten in an iterative form (with
a loop)
B. Recursive calls take time and consume additional memory
C. In general, recursive algorithms lead to better performance than
iterative algorithms
D. A recursive method is a method that calls itself
E. To terminate, a recursive algorithm must have a base case
18. Suppose we’re debugging a quicksort implementation that is supposed to sort an array in
ascending order. After the first partition step has been completed, the contents of the array are in
the following order:
3 9 1 14 17 24 22 20
Which of the following statements is correct about the partition step?
a. The pivot could have been either 14 or 17
b. The pivot could have been 14, but could not have been 17
c. The pivot could have been 17, but could not have been 14
d. Neither 14 nor 17 could have been the pivot
19. Suppose currentNode refers to a node in a linked list (using the Node class with member
variables called data
and nextNode). What statement changes currentNode so that it refers to the next node?
a) currentNode ++;
b) currentNode = nextNode;
c) currentNode += nextNode;
d) currentNode = currentNode->nextNode;
20. Which of the following statements about binary trees is NOT true?
a) Every binary tree has at least one node.
b) Every non-empty tree has exactly one root node.
c) Every node has at most two children.
d) Every non-root node has exactly one parent.
21. Suppose currentNode refers to a node in a linked list (using the Node class with member
variables called data and nextNode). What boolean expression will be true when cursor refers to
the tail node of the list?
a) (currentNode == null)
b) (currentNode->nextNode == null)
c) (nextNode.data == null)
d) (currentNode.data == 0.0)
22. Suppose we have a circular array implementation of the queue class, with ten items in the
queue stored at data[2] through data[11]. The CAPACITY is 42, i.e., the array has been declared
to be of size 42. Where does the push member function place the new entry in the array?
a) data[1]
b) data[2]
c) data[11]
d) data[12]
23. ____________________ Returns true if c is a digit or a letter and false otherwise.
a) int isalpha( int c )
b) int isalnum( int c )
c) int isxdigit( int c )
d) int isdigit( int c )
24. Five statements about B-trees are below. Four of them are correct. Which one
is INCORRECT?
(a)
(b)
(c)
(d)
All B-trees are also search trees
The word B-tree stands for balanced tree
The word B-tree also stands for binary tree
All leaves of a B-tree must be on the same level
25. To check the depth of an AVL tree following time will be taken:a) 1.66 Log2n
b) 1.44 Log2n
c) Log2 (n+1)-1
d) 1.66 Log2n (n+1)
26. There are two sorted lists L1 and L2. Their lengths are n1 and n2 respectively. You are asked
to design an algorithm to generate a sorted list L3 from L1 and L2. That is, for example,
if L1=[1,5,30] andL2=[3,6,12,24,43] then L3=[1,3,5,6,12,24,30,43]. Which of the following
methods is the most efficient?
(a)
(b)
(c)
(d)
(e)
Taking elements from L1 one by one, and inserting them into L2such that the order
of L2 remains unchanged
Appending two lists (i.e. join one list's beginning to the other one's end), then applying
quick sort to the appended list.
Using a merge method that compares two elements at the head of both lists. The
smaller one is then taken out and inserted at the end of L3 while the larger one is kept
its location unchanged. Repeat this until either L1 or L2 is empty.
Append two lists and apply Shell sort.
Append two lists and apply Selection sort.
27. When should you use a const reference parameter?
a) Whenever the parameter has huge size.
b) Whenever the parameter has huge size, the function changes the parameter within
its body, and you do NOT want these changes to alter the actual argument.
c) Whenever the parameter has huge size, the function changes the parameter within its
body, and you DO
d) want these changes to alter the actual argument.
e) Whenever the parameter has huge size, and the function does not change the parameter
within its body.
28. If f, r are the front, rear pointers respectively in a circular queue then the condition
size) % size == 1) && ( (f != 0) || (r != -1) ) represents
(a) Queue has only one element
(c) More than 2 elements but not full
(( f – r +
(b) Two Elements
(d) Impossible values
(e) Queue is full.
29. Consider the following Traveling salesperson problem instance matrix. What would be the
value of g(1, {2, 3})?
∞
1
2
(a)
(b)
(c)
(d)
(e)
8
∞
3
5
7
∞
2
12
14
17
9.
30.What is the expected time required to search for a value in a binary search tree
containing n nodes? (You should make reasonable assumptions about the structure of the
tree.)
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
e) O(n2)
31. If we use mergesort to sort an array with n elements, what is the worst case time required for
the sort?
a) O(1)
b) O(log n)
c) O(n)
d) O(n log n)
e) O(n2)
32. There are several factors that affect the efficiency of lookup operations in a hash table.
Which of the following is not one of those factors?
a) Number of elements stored in the hash table
b) Size of elements stored in the hash table
c) Number of buckets in the hash table
d) Quality of the hash function
e) All of the above factors affect the efficiency of hash table lookups
33. When we organize our data as an ordered list, what is the time complexity of
inserting/deleting a data item to/from the list?
(a)
(b)
(c)
(d)
(e)
O(length_of_list*length_of_list)
O(length_of_list)
O(log(length_of_list * length_of_list))
O(1)
O( log(length_of_list)*length_of_list ).
34. For the bubble sort algorithm, what is the time complexity of the best/worst case? (assume
that the computation stops as soon as no more swaps in one pass)
(a)
(b)
(c)
(d)
(e)
best case: O(n)
worst case: O(n*n)
best case: O(n)
worst case: O(n*log(n))
best case: O(n*log(n)) worst case: O(n*log(n))
best case: O(n*log(n)) worst case: O(n*n)
best case: O( 1) worst case: O( n ).
35. The graph coloring algorithm’s time can be bounded by _________
(a) O(mnm) (b) O(nm) (c) O(nm. 2n)
(d) O(mn.2n)
(e) O(nmn)
36. For the given 5 jobs with penalties 6,3,4,8,5 respectively, the static state space tree is
generated, then if the first job is selected but not the second job for the first two jobs
consideration, then the rank of such situational node is ______
(a) 6 (b) 3
(c) 0
(d) 9 (e) 20.
37. Suppose cursor refers to a node in a linked list, what statement changes cursor so that it refers
to the next node?
(a)
(b)
(c)
(d)
(e)
cursor++;
cursor = link;
cursor += link;
cursor = cursor->link;
(a) and (c) above.
(Note:
struct cursor {
int data;
struct cursor *link;
};
38. Four statements about trees are below. Three of them are correct. Which one is
INCORRECT?
a) Trees are recursively defined multi-dimensional data structures tree
b) The order of a tree indicates a maximum number of children allowed at each node of the
c) A search tree is a special type of tree where all values (i.e. keys) are ordered
d) If Tree1's size is greater than Tree2's size, then the height of Tree1 must also be
greater than Tree2's height.
39. Suppose a pointer has been declared in main but has not assigned any variable address then
a) That pointer points to First byte in main function
b) That pointer contains a NULL value
c) None of these
d) That pointer points to any memory address
40. Which statement of the following statements is incorrect?
a) Lists can be implemented by using arrays or linked lists
b) A list is a sequence of one or more data items
c) Stack is a special kind of list in which all insertions and deletions take place at one
end
d) Stacks are easier to implement than lists
41. If for a given ‘Queue’ initially f=0 and r=-1, then f = r refers to
(a) Queue is empty
(b) Queue is full
(c) Queue has two elements
(d) Exactly one element is there
(e) Not possible at all.
42. Which of the following statements about stacks is incorrect?
a)
b)
c)
d)
Stacks can be implemented using linked lists.
Stacks are first-in, first-out (FIFO) data structures.
New nodes can only be added to the top of the stack.
The last node (at the bottom) of a stack has a null (0) link.
43. P, Q and R are pointer variables. The statements below are intended to swap the contents of
the nodes pointed to by P and Q. rewrite it so that it will work as intended.
P = Q; R = Q; Q = R;
(a) R=Q; P=R; Q=R;
(b) R=P; P=P; Q=Q;
(c) P=P; P=Q; R=Q;
(d) R=P; P=Q; Q=R;
(e) P=R; R=Q; Q=R;
44. The statement head->Link->Link->Link = NULL terminates a linked list after its
__________ node.
(a) 2nd
(e) first.
(b) 4th
(c) 5th
(d) 3rd
45. The difference between a binary tree and a binary search tree is that ,a binary search tree has
a) two children per node whereas a binary tree can have none, one, or two children per
node
b) in binary search tree nodes are inserted based on the values they contain
c) in binary tree nodes are inserted based on the values they contain
d) none of these
46. Any string of bits of length ‘n’ represents a unique non-negative integer between
(a)
(b)
(c)
(d)
(e)
0 and 2n-1-1
0 and 2n –1
2n-1
0 and 2n
0 and 2n-1.
47. What is the best choice for the time complexity of the algorithm C in question 2?
(a)
(b)
(c)
(d)
(e)
O(n1*n2)
O(n1*log(n2))
O(n1+n2)
O(max(n1,n2))
O(log(n1)*n2).
48. Which of the following is "TRUE" about arrays,
a) We can increase the size of arrays after their creation.
b) We can decrease the size of arrays after their creation.
c) We can increase but can't decrease the size of arrays after their creation.
d) We can neither increase nor decrease the array size after their creation.
49. The number of null branches for a binary tree with 20 nodes is
(a)
(b)
(c)
(d)
(e)
21
20
22
19
18.
50. In an AVL tree to delete a parent with two childs in a straight line following rotations will be
required:a) Single
b) Double
c) Triple
d) None.
51. What is the asymptotic runtime for traversing all nodes in a binary search tree with n nodes
and printing them in order?
a)
b)
c)
d)
O(n ⋅ log(n))
O(n)
O( n)
O(log(n))
52. Assuming that the hash function for a table works well, and the size of the hash table is
reasonably large compared to the number of items in the table, the expected (average) time
needed to find an item in a hash table containing n items is
a)
b)
c)
d)
e)
O(1)
O(log n)
O(nlog n)
O(n).
O( n)
53. The time complexity of binary search in best, worst cases for the array of size
N is
(a) N, N2
(b) N, N
(c) 1, logN
(d) 1, NlogN
(e) logN, N2.
54. The time taken by NP-class sorting algorithm is
(a)
(b)
(c)
(d)
(e)
O(1)
O(log n)
O(n2)
O(n log n)
O(n).
55. An algorithm that requires __________ operations to complete its task on n data elements is
said to have a
linear runtime.
a)
b)
c)
d)
n
3n
2+3n+2
2n+1
56. Which of the following represents the efficiency of the insertion sort?
a)
b)
c)
d)
O(1)
O(log n)
O(n)
O(n2)
57. The property that is not expected from good hashing technique should
(a)
(b)
(c)
(d)
(e)
Produce keys uniformly distributed over the range
Easy to program
Produce no collisions
Produces collisions
All above properties are expected.
58. Which data structure represents a waiting line and limits insertions to be made at the back of
the data structure and limits removals to be made from the front?
a)
b)
c)
d)
Stack.
Queue.
Binary tree.
Linked list.
59. Which of the following algorithm solves the all pair shortest path problem ?
a) Dijkstra's algorithm
b) Floyd's algorithm
c) Prim's algorithm
d) Warshall's algorithm
60. This sort finds location ‘pos’ of smallest elements in a(i), …. , a(n) and then interchange
a(pos) with a(i) for i = 1, …., n – 1.
(a) Selection sort
(b) Quick sort
(c) Heap sort
(d) Bubble sort
(e) Insertion sort.
61. Suppose that the class declaration of SomeClass includes the following function prototype.
bool LessThan( SomeClass anotherObject );
Which of the following tests in the client code correctly compares two class objects alpha
and beta?
a) if (alpha < beta)
b) if (alpha.LessThan(beta))
c) if (LessThan(alpha, beta))
d) if (LessThan(alpha).beta)
62. The average successful search time for sequential search on 'n' items is ?
a) n/2
b) (n - 1)/2
c) (n + 2)/2
d) log(n) + 1
63. Which of the following is generated by Folding method?
(a)
(b)
(c)
(d)
(e)
.
A hash function
Index function for a triangular matrix
Linking the tail node to the head node in linked list
Linear probing
Chaining.
64. Consider a tree ‘t’ has two subtrees t1 and t2. Identify the tree where the subtree
t1 has minimum height and the subtree t2 has maximum height.
(a)
(b)
(c)
(d)
(e)
Fibonacci
B+
Sparse
Complete binary
B.
65. Consider the following function:
void test_a(int n)
{
cout << n << " ";
if (n>0)
test_a(n-2);
}
What is printed by the call test_a(4)?
a) 4 2 0
b) 0 2 4
c) 0 2
d) 2 4
66.
Breadth
First
Search
is
used
in
a) Binary trees
b) Stacks
c) Graphs
d) Both a and c above
66. The number of arguments supplied from the command line, by conversion is known as
?
a)
b)
c)
d)
arg c
arg v
#define
#include
67. Which of the following statements hold true for binary trees?
a) The left subtree of a node contains only nodes with keys less than the node’s key
b) The right subtree of a node contains only one nodes with key greater than the node’s key.
c) Both a and b above
d) Noth left and right subtree nodes contains only nodes with keys less than the node’s key
68. Which of the following linked list below have last node of the list pointing to the first node?
a) circular doubly linked list
b) circular linked list
c) circular singly linked list
d) doubly linked list
70.
Which
a)
b)
c)
d)
71.
of
the
following
ways
below
is
a
In
order
traversal?
Root->left sub tree->right sub tree
Root-> right sub tree ->left sub tree
right sub tree->left sub tree->Root
left sub tree->right sub tree->Root
The
time
required
in
best
case
for
search
operation
in
binary
tree
is
a) O(n)
b) O(log n)
c) O(2n)
d) O(log 2n)
72. In ______________ tree, the heights of two child subtree of any node differ by at most one
a) Binary tree
b) Red black tree
c) Splay tree
d) AVL tree
73.
73. If data is a circular array of CAPACITY elements, and R is an rear index into that
array, what is the formula for the index after R?
(a) (R + 1) % CAPACITY
(b) R % (1 + CAPACITY)
(c) (R % 1) + CAPACITY
(d) R + (1 % CAPACITY)
(e) R = R + 1.
74. In a graph if e=(u, v) means
a)
b)
c)
d)
u is adjacent to v but v is not adjacent to u
e begins at u and ends at v
u is processor and v is successor
both b and c
75.
What kind of list is best to answer questions such as "What is the item at position n?"
(a) Lists implemented with an array
(b) Doubly-linked lists
(c) Singly-linked lists
(d) Doubly-linked or singly-linked lists are equally best
(e) Circular linked list implemented with priorities.
76. In a graph if e=[u, v], Then u and v are called
a)
b)
c)
d)
endpoints of e
adjacent nodes
neighbors
all of above
77. In a Heap tree
a) Values in a node is greater than every value in left sub tree and smaller than right sub tree
b) Values in a node is greater than every value in children of it
c) Both of above conditions applies
d) None of above conditions applies
78.
78. With the “wrap around” implementation of a queue, which of the following code should
be used to work out the next location of deletion?
(a) front++
(b) front-(c) front = (front % max_queue_size) + 1
(d) front = (front ++) % max_queue_size
(e) front = 0.
79. In a binary tree, certain null entries are replaced by special pointers which point to nodes
higher in the tree for efficiency. These special pointers are called
a) Leaf
b) Branch
c) Path
d) thread
80.
80. I have implemented the queue with a circular array, keeping track of first, last,
and count (the number of items in the array). Suppose first is zero, and last is SIZE-1.
What can you tell me about count?
(a) count must be zero.
(b) count must be SIZE
(c) count must be SIZE-2
(d) count must be SIZE+1
(e) count could be zero or SIZE, but no other values could occur.
81. Which of the following sorting algorithm is of divide-and-conquer type?
a) Bubble sort
b) Insertion sort
c) Quick sort
d) All of above
82. The post order traversal of a binary tree is DEBFCA. Find out the pre order traversal
a) ABFCDE
b) ADBFEC
c) ABDECF
d) ABDCEF
84.
The time complexity of binary search in best, worst cases for an array of size N is
(a) N, N2
(b) N, N
(c) Log N, N2
(d) 1, N log N
(e) 1, log N.
85. A binary tree can easily be converted into q 2-tree
a)
b)
c)
d)
by replacing each empty sub tree by a new internal node
by inserting an internal nodes for non-empty node
by inserting an external nodes for non-empty node
by replacing each empty sub tree by a new external node
86. When representing any algebraic expression E which uses only binary operations in a 2-tree,
a) the variable in E will appear as external nodes and operations in internal nodes
b) the operations in E will appear as external nodes and variables in internal nodes
c) the variables and operations in E will appear only in internal nodes
d) the variables and operations in E will appear only in external nodes
87. The depth of a complete binary tree is given by
a)
b)
c)
d)
Dn = n log2n
Dn = n log2n+1
Dn = log2n
Dn = log2n+1
88. A binary tree whose every node has either zero or two children is called
a)
b)
c)
d)
Complete binary tree
Binary search tree
Extended binary tree
None of above
89. Which of the following formulas in Omega notation best represent the expression
n²+35n+6?
(a) Ω (n³)
(b) Ω (n²)
(c) Ω (n)
(d) Ω (35)
(e) Ω (6).
90. What is the correct post fix form of the given infix expression a+b/(c+d)%e*(gh/(i+j)*k)?
(a) abcd+/e%ghij+/k**-+
(c) ab+cd/e%ghij+/k**(e) abcd+/e%ghij+/k*-*+.
(b) abcd+e/%ghij+/k**-+
(d) ab+cd+/e%ghij+/k*-*
91. Which of the following data structure is non-linear type?
a) Strings
b) Lists
c) Stacks
d) None of above
92. Identify the data structure which allows deletions at both ends of the list but insertion at only
one end.
a) Input-restricted deque
b) Output-restricted deque
c) Priority queues
d) None of above
93. Which data structure allows deleting data elements from front and inserting at rear?
a) Stacks
b) Queues
c) Deques
d) Binary search tree
94. The memory address of fifth element of an array can be calculated by the formula
a) LOC(Array[5]=Base(Array)+w(5-lower bound), where w is the number of words per
memory cell for the array
b) LOC(Array[5])=Base(Array[5])+(5-lower bound), where w is the number of words per
memory cell for the array
c) LOC(Array[5])=Base(Array[4])+(5-Upper bound), where w is the number of words per
memory cell for the array
d) None of above
95. Which of the following is not the required condition for binary search algorithm?
a)
b)
c)
d)
The list must be sorted
there should be the direct access to the middle element in any sublist
There must be mechanism to delete and/or insert elements in list
none of above
96. Sequential search has a time complexity of O(n), and binary search has a time
complexity ofO(log(n)). What difference will it make when the size n is 1000?
(a) You would not notice much difference because computers run very fast anyway
(b) As n is 1000, binary search is twice as fast as sequential search
(c) As n is 1000, binary search is 10 times as fast as sequential search
(d) As n is 1000, binary search is 10 times as slow as sequential search
(e) As n is 1000, binary search is 100 times as fast as sequential search.
97. Which of the following statement is false?
a)
b)
c)
d)
Arrays are dense lists and static data structure
data elements in linked list need not be stored in adjecent space in memory
pointers store the next data element of a list
linked lists are collection of the nodes that contain information part and next pointer
98.
98. Express the formula (n - 2)*(n - 4) using θ notation:
(a) θ (n2)
(b) θ (8)
(c) θ (log n)
(d) θ (n)
(e) θ (1).
99.
For a binary tree the In-order traversal was found to be as HIGCEFD. Once the post-order
traversal was conducted the output is IHGFEDC. What is the per-order for the given tree?
(a) CGDHEIF
(b) GHICDEF
a) (d) CDEFGHI
answered.
(c) CGHIDEF
(e) Data insufficient and hence can’t be
100. How many minimum number of spanning trees, one can have from a given connected
graph with N nodes is having different weights for the edges.
(a) N-1
`
(b) One
(c) 1/(N+1) 2NCN
(d) 2NCN
(e) N.