Data Structure
Data Structure
Answer: c
Explana on: A data structure is a way to store and organize data efficiently, enhancing access and
manipula on, unlike programming languages, algorithms, or computer hardware.
Answer: d
Explana on: Arrays are of fixed size. If we insert elements less than the allocated size, unoccupied
posi ons can’t be used again. Wastage will occur in memory.
Answer: a
Explana on: Stacks are used for the implementa on of Recursion.
4. The data structure required to check whether an expression contains a balanced parenthesis is?
a) Queue
b) Stack
c) Tree
d) Array
View Answer
Answer: b
Explana on: The stack is a simple data structure in which elements are added and removed based on
the LIFO principle. Open parenthesis is pushed into the stack and a closed parenthesis pops out
elements ll the top element of the stack is its corresponding open parenthesis. If the stack is empty,
parenthesis is balanced otherwise it is unbalanced.
Answer: a
Explana on: Data transfer between the two asynchronous process uses the queue data structure for
synchronisa on. The rest are all stack applica ons.
adver sement
6. Which data structure is needed to convert infix nota on to pos ix nota on?
a) Tree
b) Branch
c) Stack
d) Queue
View Answer
Answer: c
Explana on: The Stack data structure is used to convert infix expression to pos ix expression. The
purpose of stack is to reverse the order of the operators in the expression. It also serves as a storage
structure, as no operator can be printed un l both of its operands have appeared.
Answer: b
Explana on: Pos ix Expression is (6*(3-(2+4))) which results -18 as output.
8. What data structure would you mostly likely see in non recursive implementa on of a recursive
algorithm?
a) Stack
b) Linked List
c) Tree
d) Queue
View Answer
Answer: a
Explana on: In recursive algorithms, the order in which the recursive process comes back is the reverse
of the order in which it goes forward during execu on. The compiler uses the stack data structure to
implement recursion. In the forwarding phase, the values of local variables, parameters and the return
address are pushed into the stack at each recursion level. In the backing-out phase, the stacked address
is popped and used to execute the rest of the code.
9. Which of the following statement(s) about stack data structure is/are NOT correct?
a) Top of the Stack always contain the new node
b) Stack is the FIFO data structure
c) Null link is present in the last node at the bo om of the stack
d) Linked List are used for implemen ng Stacks
View Answer
Answer: b
Explana on: Stack follows LIFO.
10. The data structure required for Breadth First Traversal on a graph is?
a) Array
b) Stack
c) Tree
d) Queue
View Answer
Answer: d
Explana on: In Breadth First Search Traversal, BFS, star ng vertex is first taken and adjacent ver ces
which are unvisited are also taken. Again, the first vertex which was added as an unvisited adjacent
vertex list will be considered to add further unvisited ver ces of the graph. To get the first unvisited
vertex we need to follows First In First Out principle. Queue uses FIFO principle.
Answer: a
Explana on: Infix Expression is A-B/(C*D^E)
This can be wri en as: A-(B/(C*(D^E)))
Thus prefix expression is -A/B*C^DE.
12. Which of the following points is/are not true about Linked List data structure when it is compared
with an array?
a) Random access is not allowed in a typical implementa on of Linked Lists
b) Access of elements in linked list takes less me than compared to arrays
c) Arrays have be er cache locality that can make them be er in terms of performance
d) It is easy to insert and delete elements in Linked List
View Answer
Answer: b
Explana on: To access an element in a linked list, we need to traverse every element un l we reach the
desired element. This will take more me than arrays as arrays provide random access to its elements.
13. Which data structure is based on the Last In First Out (LIFO) principle?
a) Tree
b) Linked List
c) Stack
d) Queue
View Answer
Answer: c
Explana on: The data structure that follows the Last In First Out (LIFO) principle is the Stack. It operates
like a stack of objects, making it suitable for specific-order management.
14. Which of the following applica on makes use of a circular linked list?
a) Recursive func on calls
b) Undo opera on in a text editor
c) Implement Hash Tables
d) Alloca ng CPU to resources
View Answer
Answer: d
Explana on: Generally, round robin fashion is employed to allocate CPU me to resources which makes
use of the circular linked list data structure. Recursive func on calls use stack data structure. Undo
Opera on in text editor uses doubly linked lists. Hash tables uses singly linked lists.
Answer: a
Explana on: It compactly stores bits and exploits bit-level parallelism.
16. Which of the following tree data structures is not a balanced binary tree?
a) Splay tree
b) B-tree
c) AVL tree
d) Red-black tree
View Answer
Answer: b
Explana on: All the tree data structures given in op ons are balanced, but B-tree can have more than
two children.
17. Which of the following is not the type of queue?
a) Priority queue
b) Circular queue
c) Single ended queue
d) Ordinary queue
View Answer
Answer: c
Explana on: Queue always has two ends. So, single ended queue is not the type of queue.
18. Which of the following data structures can be used for parentheses matching?
a) n-ary tree
b) queue
c) priority queue
d) stack
View Answer
Answer: d
Explana on: For every opening brace, push it into the stack, and for every closing brace, pop it off the
stack. Do not take ac on for any other character. In the end, if the stack is empty, then the input has
balanced parentheses.
Answer: b
Explana on: Top tree is a type of data structure which is based on unrooted dynamic binary tree and is
used to solve path related problems. It allows an algorithm called divide and conquer.
Answer: c
Explana on: In a linear queue, dequeue opera on causes the star ng elements of the array to be
empty, and there is no way you can use that space, while in a circular queue, you can effec vely use that
space. Priority queue is used to delete the elements based on their priority. Higher priority elements will
be deleted first whereas lower priority elements will be deleted next. Queue data structure always
follows FIFO principle.
21. Which of the following is the most widely used external memory data structure?
a) B-tree
b) Red-black tree
c) AVL tree
d) Both AVL tree and Red-black tree
View Answer
Answer: a
Explana on: In external memory, the data is transferred in form of blocks. These blocks have data
valued and pointers. And B-tree can hold both the data values and pointers. So B-tree is used as an
external memory data structure.
Answer: d
Explana on: Array is a linear data structure. Strings are a collec on and sequence of codes, alphabets or
characters. Linked List is a linear data structure having a node containing data input and the address of
the next node. The cord is also known as the rope data structure.
main()
int i;
for(i=0;i<len;i++)
for(i=0;i<len;i++)
a) yrdnuof nas
b) foundry nas
c) sanfoundry
d) san foundry
View Answer
Answer: a
Explana on: First, the string ‘san foundry’ is pushed one by one into the stack.
When it is popped, the output will be as ‘yrdnuof nas’.
24. Which of the following data structure can provide efficient searching of the elements?
a) binary search tree
b) unordered lists
c) 2-3 tree
d) treap
View Answer
Answer: c
Explana on: The average case me for lookup in a binary search tree, treap and 2-3 tree is O(log n) and
in unordered lists it is O(n). But in the worst case, only the 2-3 trees perform lookup efficiently as it takes
O(log n), while others take O(n).
Answer: b
Explana on: It is a self balancing tree with height difference atmost 1.
26. What is the me complexity for searching a key or integer in Van Emde Boas data structure?
a) O (M!)
b) O (log M!)
c) O (log (log M))
d) O (M2)
View Answer
Answer: c
Explana on: In order to search a key or integer in the Van Emde Boas data structure, the opera on can
be performed on an associa ve array. Hence, the me complexity for searching a key or integer in Van
Emde Boas data structure is O (log (log M)).
27. The op mal data structure used to solve Tower of Hanoi is _________
a) Tree
b) Heap
c) Priority queue
d) Stack
View Answer
Answer: d
Explana on: The Tower of Hanoi involves moving of disks ‘stacked’ at one peg to another peg with
respect to the size constraint. It is conveniently done using stacks and priority queues. Stack approach is
widely used to solve Tower of Hanoi.
Answer: b
Explana on: Bin data structure allows us to have efficient region queries. A frequency of bin is increased
by one each me a data point falls into a bin.
29. Which is the most appropriate data structure for reversing a word?
a) stack
b) queue
c) graph
d) tree
View Answer
Answer: a
Explana on: Stack is the most appropriate data structure for reversing a word because stack follows
LIFO principle.
if(size == 0)
System.out.println("underflow");
else
while(current != null)
System.out.println(current.getEle());
current = current.getNext();
}
}
Answer: a
Explana on: An alias of the node ‘first’ is created which traverses through the list and displays the
elements.
31. Which of the following is the simplest data structure that supports range searching?
a) AA-trees
b) K-d trees
c) Heaps
d) binary search trees
View Answer
Answer: b
Explana on: K-d trees are the simplest data structure that supports range searching and also it achieves
the respectable running me.
Answer: b
Explana on: Hash table is a data structure that has an advantage that it allows fast access of elements.
Hash func ons are used to determine the index of any input record in a hash table.
Answer: d
Explana on: Ternary heap is a type of data structure in the field of computer science. It is a part of the
Heap data structure family. It is a priority queue type of data structure that follows all the property of
heap.
34. What is a dequeue?
a) A queue implemented with both singly and doubly linked lists
b) A queue with insert/delete defined for front side of the queue
c) A queue with insert/delete defined for both front and rear ends of the queue
d) A queue implemented with a doubly linked list
View Answer
Answer: c
Explana on: A dequeue or a double ended queue is a queue with insert/delete defined for both front
and rear ends of the queue.
35. A data structure in which elements can be inserted or deleted at/from both ends but not in the
middle is?
a) Priority queue
b) Dequeue
c) Circular queue
d) Queue
View Answer
Answer: b
Explana on: In dequeuer, we can insert or delete elements from both the ends. In queue, we will follow
first in first out principle for inser on and dele on of elements. Element with least priority will be
deleted in a priority queue.
System.out.println(arr[2]);
System.out.println(arr[4]);
a) 4 and 2
b) 2 and 4
c) 5 and 3
d) 3 and 5
View Answer
Answer: d
Explana on: Array indexing starts from 0.
Answer: a
Explana on: Dele on becomes easier with doubly linked list, hence it is appropriate.
This set of Data Structure Mul ple Choice Ques ons & Answers (MCQs) focuses on “Array and Array
Opera ons”.
Answer: b
Explana on: Array contains elements only of the same type.
Answer: c
Explana on: This is the syntax to ini alize an array in C.
Answer: c
Explana on: Note that int arr[]; is declara on whereas int arr[] = new int[3]; is to instan ate an array.
adver sement
4. Which of the following is the correct way to declare a mul dimensional array in Java?
a) int[] arr;
b) int arr[[]];
c) int[][]arr;
d) int[[]] arr;
View Answer
Answer: c
Explana on: The syntax to declare mul dimensional array in java is either int[][] arr; or int arr[][];
System.out.println(arr[2]);
System.out.println(arr[4]);
a) 3 and 5
b) 5 and 3
c) 2 and 4
d) 4 and 2
View Answer
Answer: a
Explana on: Array indexing starts from 0.
System.out.println(arr[5]);
}
a) 4
b) 5
c) ArrayIndexOutOfBoundsExcep on
d) InavlidInputExcep on
View Answer
Answer: c
Explana on: Trying to access an element beyond the limits of an array gives
ArrayIndexOutOfBoundsExcep on.
Answer: b
Explana on: ArrayIndexOutOfBoundsExcep on is a run- me excep on and the compila on is error-free.
Answer: d
Explana on: Whenever a par cular memory loca on is referred to, it is likely that the loca ons nearby
are also referred, arrays are stored as con guous blocks in memory, so if you want to access array
elements, spa al locality makes it to access quickly.
Answer: d
Explana on: Arrays store elements of the same data type and present in con nuous memory loca ons.
Answer: b
Explana on: Arrays are of fixed size. If we insert elements less than the allocated size, unoccupied
posi ons can’t be used again. Wastage will occur in memory.
Answer: d
Explana on: Since there are 15 int elements and each int is of 4bytes, we get 15*4 = 60bytes.
Answer: a
Explana on: In general, Array Indexing starts from 0. Thus, the index of the first element in an array is 0.
Answer: a
Explana on: Elements in an array are accessed randomly. In Linked lists, elements are accessed
sequen ally.
This set of Data Structure Mul ple Choice Ques ons & Answers (MCQs) focuses on “Searching”.
Answer: d
Explana on: It is prac cal to implement linear search in the situa ons men oned in When the list has
only a few elements and When performing a single search in an unordered list, but for larger elements
the complexity becomes larger and it makes sense to sort the list and employ binary search or hashing.
Answer: c
Explana on: This gives a very slight improvement as you are skipping the first k elements.
adver sement
4. Which of the following searching algorithm is used with exponen al sort a er finding the appropriate
range?
a) Jump search
b) Fibonacci Search
c) Linear search
d) Binary search
View Answer
Answer: d
Explana on: In exponen al search, we first find a range where the required elements should be present
in the array. Then we apply binary search in this range.
5. Which of the following searching algorithm is fastest when the input array is not sorted but has
uniformly distributed values?
a) linear search
b) jump search
c) interpola on search
d) binary search
View Answer
Answer: a
Explana on: Out of the given op ons linear search is the only searching algorithm which can be applied
to arrays which are not sorted. It has a me complexity of O(n) in the worst case.
6. What is the me complexity of Z algorithm for pa ern searching (m = length of text, n = length of
pa ern)?
a) O(n)
b) O(m)
c) O(n + m)
d) O(m * n)
View Answer
Answer: c
Explana on: Z algorithm is an efficient pa ern searching algorithm as it searches the pa ern in linear
me. It has a me complexity of O(m + n) where m is the length of text and n is the length of the
pa ern.
7. In which of the cases uniform binary search fails compared to binary search?
a) Complexity of code
b) Many searches will be performed on several arrays of the same length
c) Many searches will be performed on the same array
d) A table lookup is generally faster than an addi on and a shi
View Answer
Answer: a
Explana on: Uniform binary search code is more complex to implement than binary search as it involves
mid points to be computed in hand before search.
Answer: c
Explana on: Interpola on search is a varia on of binary search which gives the best result when the
array has uniformly distributed values. Interpola on search goes to different posi ons depending on the
value being searched whereas binary search always goes to the middle element.
10. Which of the following step is taken a er finding an element having value greater than the element
being searched?
a) binary search takes place in the forward direc on
b) binary search takes place in a backward direc on
c) linear search takes place in the forward direc on
d) linear search takes place in the backward direc on
View Answer
Answer: d
Explana on: First an element having value greater than the element being searched is found. A er this
linear search is performed in a backward direc on.
Answer: b
Explana on: When the speed of access depends on the loca on previously accessed, Fibonacci search is
be er compared to binary search as it performs well on those loca ons which have lower dispersion.
Fibonacci search won’t work on unsorted arrays. The input should be a sorted array or array should be
sorted before Fibonacci search.
12. In which of the following case jump search will be preferred over exponen al search?
a) when the given array is very small in size
b) when the given array is very large in size
c) jumping backwards takes significantly more me than jumping forward
d) jumping forward takes significantly more me than jumping backwards
View Answer
Answer: d
Explana on: Jump search only needs to jump backwards once, while an exponen al search can jump
backwards up to log n mes. Thus jump search will be preferred if jumping backwards is expensive.
This set of Data Structures & Algorithms Mul ple Choice Ques ons & Answers (MCQs) focuses on
“Linear Search Recursive”.
1. Is there any difference in the speed of execu on between linear serach(recursive) vs linear
search(ltera ve)?
a) Both execute at same speed
b) Linear search(recursive) is faster
c) Linear search(Itera ve) is faster
d) Cant be said
View Answer
Answer: c
Explana on: The Itera ve algorithm is faster than the la er as recursive algorithm has overheads like
calling func on and registering stacks repeatedly.
2. Is the space consumed by the linear search(recursive) and linear search(itera ve) same?
a) No, recursive algorithm consumes more space
b) No, recursive algorithm consumes less space
c) Yes
d) Nothing can be said
View Answer
Answer: a
Explana on: The recursive algorithm consumes more space as it involves the usage the stack space(calls
the func on numerous mes).
Answer: a
Explana on: In the worst case scenario, there might be a need of calling the stack n mes. Therfore
O(n).
adver sement
Answer: a
Explana on: It is used when the size of the dataset is low as its run me is O(n) which is more when
compared to the binary search O(logn).
5. The array is as follows: 1,2,3,6,8,10. At what me the element 6 is found? (By using linear
search(recursive) algorithm)
a) 4th call
b) 3rd call
c) 6th call
d) 5th call
View Answer
Answer: a
Explana on: Provided that the search starts from the first element, the func on calls itself ll the
element is found. In this case, the element is found in 4th call.
Sanfoundry Cer fica on Contest of the Month is Live. 100+ Subjects. Par cipate Now!
6. The array is as follows: 1,2,3,6,8,10. Given that the number 17 is to be searched. At which call it tells
that there’s no such element? (By using linear search(recursive) algorithm)
a) 7th call
b) 9th call
c) 17th call
d) The func on calls itself infinite number of mes
View Answer
Answer: a
Explana on: The func on calls itself ll the element is found. But at the 7th call it terminates as goes
outside the array.
7. What is the best case run me of linear search(recursive) algorithm on an ordered set of elements?
a) O(1)
b) O(n)
c) O(logn)
d) O(nx)
View Answer
Answer: a
Explana on: The best case occurs when the given element to be found is at the first posi on. Therefore
O(1) is the correct answer.
for(i=0;i<n;i++)
if(a[i]==key)
b)
LinearSearch(int[] a, n,key)
{
if(n<1)
return False
if(a[n]==key)
return True
else
LinearSearch(a,n-1,key)
c)
LinearSearch(int[] a, n,key)
if(n<1)
return True
if(a[n]==key)
return False
else
LinearSearch(a,n-1,key)
d)
LinearSearch(int[] a, n,key)
if(n<1)
return False
if(a[n]==key)
return True
else
LinearSearch(a,n+1,key)
View Answer
Answer: b
Explana on: Compare n with first element in arr[]. If element is found at first posi on, return it. Else
recur for remaining array and n.
9. Can linear search recursive algorithm and binary search recursive algorithm be performed on an
unordered list?
a) Binary search can’t be used
b) Linear search can’t be used
c) Both cannot be used
d) Both can be used
View Answer
Answer: a
Explana on: As binary search requires comparison, it is required that the list be ordered. Whereas this
doesn’t ma er for linear search.
10. What is the recurrence rela on for the linear search recursive algorithm?
a) T(n-2)+c
b) 2T(n-1)+c
c) T(n-1)+c
d) T(n+1)+c
View Answer
Answer: c
Explana on: A er each call in the recursive algorithm, the size of n is reduced by 1. Therefore the
op mal solu on is T(n-1)+c.
This set of Data Structure Mul ple Choice Ques ons & Answers (MCQs) focuses on “Sor ng”.
1. Which of the following sor ng algorithms is the fastest for sor ng small arrays?
a) Quick sort
b) Shell sort
c) Inser on sort
d) Heap sort
View Answer
Answer: c
Explana on: For sor ng small arrays, inser on sort runs even faster than quick sort. But, it is imprac cal
to sort large arrays.
Answer: d
Explana on: Since selec on sort is an in-place sor ng algorithm, it does not require addi onal storage.
Answer: b
Explana on: Merge sort algorithm divides the array into two halves and applies merge sort algorithm to
each half individually a er which the two sorted halves are merged together. Thus its method of sor ng
is called merging.
adver sement
Answer: a
Explana on: Bo om up merge sort uses the itera ve method in order to implement sor ng. It begins by
merging a pair of adjacent array of size 1 each and then merge arrays of size 2 each in the next step and
so on.
5. Merge sort uses which of the following method to implement sor ng?
a) selec on
b) exchanging
c) merging
d) par oning
View Answer
Answer: c
Explana on: Merge sort implements sor ng by merging the sorted versions of smaller parts of the array.
Thus its method of sor ng is called merging.
Answer: d
Explana on: Quick sort is the fastest known sor ng algorithm because of its highly op mized inner loop.
Answer: c
Explana on: Shell sort is an example of internal sor ng because sor ng of elements is done internally
using an array.
8. Quick sort uses which of the following method to implement sor ng?
a) par oning
b) selec on
c) exchanging
d) merging
View Answer
Answer: a
Explana on: Quick sort makes par ons of the input array about the pivot in order to implement
sor ng. Thus its method of sor ng is called par oning.
9. In heap sort, a er dele ng the last minimum element, the array will contain elements in?
a) increasing sor ng order
b) tree preorder
c) tree inorder
d) decreasing sor ng order
View Answer
Answer: d
Explana on: By logic, a er dele ng minimum element, the heap will contain elements in decreasing
sor ng order. We can change this by altering the ordering property.
Answer: b
Explana on: Out of the given op ons Tim sort is the only algorithm which is stable. As both cons tuents
of Tim sort (I.e inser on sort and merge sort) are stable so Tim sort also becomes stable.
12. Which of the following sor ng algorithm uses the method of inser on?
a) selec on sort
b) quick sort
c) bubble sort
d) cycle sort
View Answer
Answer: d
Explana on: Cycle sort is the only algorithm from the given ones that uses the method of inser on.
Other than this, inser on sort also uses the method of inser on for sor ng.
Answer: a
Explana on: Gnome sort and merge sort are stable sor ng algorithms as the elements with iden cal
values appear in the same order in the output array as they were in the input array when any of these
sor ng algorithms are implemented.
This set of Data Structures & Algorithms Mul ple Choice Ques ons & Answers (MCQs) focuses on
“Inser on Sort”.
2. Which of the following algorithm implementa ons is similar to that of an inser on sort?
a) Binary heap
b) Quick sort
c) Merge sort
d) Radix sort
View Answer
Answer: a
Explana on: Inser on sort is similar to that of a binary heap algorithm because of the use of temporary
variable to swap.
Answer: d
Explana on: The average case analysis of a ght bound algorithm is mathema cally achieved to be
O(N2).
adver sement
4. Any algorithm that sorts by exchanging adjacent elements require O(N 2) on average.
a) True
b) False
View Answer
Answer: a
Explana on: Each swap removes only one inversion, so O(N2) swaps are required.
Answer: a
Explana on: The total number of pairs in a list L is N(N-1)/2. Thus, an average list has half this amount,
or N(N-1)/4 inversions.
Answer: c
Explana on: If the input is pre-sorted, the running me is O(N), because the test in the inner for loop
always fails immediately and the algorithm will run quickly.
7. What will be the number of passes to sort the elements using inser on sort?
14, 12,16, 6, 3, 10
a) 6
b) 5
c) 7
d) 1
View Answer
Answer: b
Explana on: The number of passes is given by N-1. Here, N=6. Therefore,
6-1=5 passes.
8. For the following ques on, how will the array elements look like a er second pass?
34, 8, 64, 51, 32, 21
a) 8, 21, 32, 34, 51, 64
b) 8, 32, 34, 51, 64, 21
c) 8, 34, 51, 64, 32, 21
d) 8, 34, 64, 51, 32, 21
View Answer
Answer: d
Explana on: A er swapping elements in the second pass, the array will look like, 8, 34, 64, 51, 32, 21.
Answer: a
Explana on: Arranging a pack of cards mimics an inser on sort. Database scenario is an example for
merge sort, arranging books is a stack and real- me systems uses quick sort.
10. In C, what are the basic loops required to perform an inser on sort?
a) do- while
b) if else
c) for and while
d) for and if
View Answer
Answer: c
Explana on: To perform an inser on sort, we use two basic loops- an outer for loop and an inner while
loop.
11. Binary search can be used in an inser on sort algorithm to reduce the number of comparisons.
a) True
b) False
View Answer
Answer: a
Explana on: Binary search can be used in an inser on sort algorithm to reduce the number of
comparisons. This is called a Binary inser on sort.
12. Which of the following op ons contain the correct feature of an inser on sort algorithm?
a) an -adap ve
b) dependable
c) stable, not in-place
d) stable, adap ve
View Answer
Answer: d
Explana on: An inser on sort is stable, adap ve, in-place and incremental in nature.
13. Which of the following sor ng algorithms is the fastest for sor ng small arrays?
a) Quick sort
b) Inser on sort
c) Shell sort
d) Heap sort
View Answer
Answer: b
Explana on: For sor ng small arrays, inser on sort runs even faster than quick sort. But, it is imprac cal
to sort large arrays.
14. For the best case input, the running me of an inser on sort algorithm is?
a) Linear
b) Binary
c) Quadra c
d) Depends on the input
View Answer
Answer: a
Explana on: The best case input for an inser on sort algorithm runs in linear me and is given by O(N).
15. Which of the following examples represent the worst case input for an inser on sort?
a) array in sorted order
b) array sorted in reverse order
c) normal unsorted array
d) large array
View Answer
Answer: b
Explana on: The worst case input for an inser on sort algorithm will be an array sorted in reverse order
and its running me is quadra c.
This set of Data Structures & Algorithms Mul ple Choice Ques ons & Answers (MCQs) focuses on
“Inser on Sort – 2”.
Answer: a
Explana on: During inser on sort, the rela ve order of elements is not changed. Therefore, it is a stable
sor ng algorithm. And inser on sort requires only O(1) of addi onal memory space. Therefore, it sorts
In-place.
2. Which of the following sor ng algorithm is best suited if the elements are already sorted?
a) Heap Sort
b) Quick Sort
c) Inser on Sort
d) Merge Sort
View Answer
Answer: c
Explana on: The best case running me of the inser on sort is O(n). The best case occurs when the
input array is already sorted. As the elements are already sorted, only one comparison is made on each
pass, so that the me required is O(n).
3. The worst case me complexity of inser on sort is O(n2). What will be the worst case me complexity
of inser on sort if the correct posi on for inser ng element is calculated using binary search?
a) O(nlogn)
b) O(n2)
c) O(n)
d) O(logn)
View Answer
Answer: b
Explana on: The use of binary search reduces the me of finding the correct posi on from O(n) to
O(logn). But the worst case of inser on sort remains O(n2) because of the series of swapping opera ons
required for each inser on.
adver sement
Answer: a
Explana on: In the incremental algorithms, the complicated structure on n items is built by first building
it on n − 1 items. And then we make the necessary changes to fix things in adding the last item. Inser on
sort builds the sorted sequence one element at a me. Therefore, it is an example of an incremental
algorithm.
int i, j, value;
value = arr[i];
j = i;
while (________ )
j = j − 1;
arr[j] = value;
Answer: b
Explana on: In inser on sort, the element is A[j] is inserted into the correct posi on in the sorted
sequence A[1… j – 1]. So, condi on given in (j > 0) && (arr[j − 1] > value) will implement while loop
correctly.
6. Which of the following is good for sor ng arrays having less than 100 elements?
a) Quick Sort
b) Selec on Sort
c) Merge Sort
d) Inser on Sort
View Answer
Answer: d
Explana on: The inser on sort is good for sor ng small arrays. It sorts smaller arrays faster than any
other sor ng algorithm.
7. Consider an array of length 5, arr[5] = {9,7,4,2,1}. What are the steps of inser ons done while running
inser on sort on the array?
a) 7 9 4 2 1 4 7 9 2 1 2 4 7 9 1 1 2 4 7 9
b) 9 7 4 1 2 9 7 1 2 4 9 1 2 4 7 1 2 4 7 9
c) 7 4 2 1 9 4 2 1 9 7 2 1 9 7 4 1 9 7 4 2
d) 7 9 4 2 1 2 4 7 9 1 4 7 9 2 1 1 2 4 7 9
View Answer
Answer: a
Explana on: The steps performed while running inser on sort on given array are:
Ini al : 9 7 4 2 1 key = 7
7 9 4 2 1 key = 4
4 7 9 2 1 key = 2
2 4 7 9 1 key = 1
12479
In each step, the key is the element that is compared with the elements present at the le side to it.
8. Statement 1: In inser on sort, a er m passes through the array, the first m elements are in sorted
order.
Statement 2: And these elements are the m smallest elements in the array.
a) Both the statements are true
b) Statement 1 is true but statement 2 is false
c) Statement 1 is false but statement 2 is true
d) Both the statements are false
View Answer
Answer: b
Explana on: In inser on sort, a er m passes through the array, the first m elements are in sorted order
but they are whatever the first m elements were in the unsorted array.
9. In inser on sort, the average number of comparisons required to place the 7 th element into its correct
posi on is ____
a) 9
b) 4
c) 7
d) 14
View Answer
Answer: b
Explana on: On average (k + 1) / 2 comparisons are required to place the k th element into its correct
posi on. Therefore, average number of comparisons required for 7th element = (7 + 1)/2 = 4.
Answer: d
Explana on: In Exchange sorts, we compare each element of an array and swap those elements that are
not in their proper posi on. Bubble Sort and Quick Sort are exchange sorts. Quick Sort is also called as
Par on-exchange Sort. Inser on sort is not an exchange sort.
This set of Data Structure Mul ple Choice Ques ons & Answers (MCQs) focuses on “Selec on Sort”.
Answer: a
Explana on: Auxiliary memory is required for storing the data temporarily.
Answer: d
Explana on: Selec on sort creates a sub-list, LHS of the ‘min’ element is already sorted and RHS is yet to
be sorted. Star ng with the first element the ‘min’ element moves towards the final element.
adver sement
int min;
min = j;
min = k;
arr[min] = arr[j];
arr[j] = temp;
b)
Subscribe Now: Design and Analysis of Algorithms Newsle er | Important Subjects Newsle ers
int min;
min = j;
min = k;
arr[min] = arr[j];
arr[j] = temp;
c)
int min;
min = j;
min = k;
arr[min] = arr[j];
arr[j] = temp;
d)
int min;
{
min = j;
min = k;
arr[min] = arr[j];
arr[j] = temp;
View Answer
Answer: a
Explana on: Star ng with the first element as ‘min’ element, selec on sort loops through the list to
select the least element which is then swapped with the ‘min’ element.
Answer: a
Explana on: Since selec on sort is an in-place sor ng algorithm, it does not require addi onal storage.
Answer: d
Explana on: In the average case, even if the input is par ally sorted, selec on sort behaves as if the
en re array is not sorted. Selec on sort is insensi ve to input.
Answer: b
Explana on: As the input size increases, the performance of selec on sort decreases.
8. The given array is arr = {3,4,5,2,1}. The number of itera ons in bubble sort and selec on sort
respec vely are __________
a) 5 and 4
b) 4 and 5
c) 2 and 4
d) 2 and 5
View Answer
Answer: a
Explana on: Since the input array is not sorted, bubble sort takes 5 itera ons and selec on sort takes
4(n-1) itera ons.
9. The given array is arr = {1,2,3,4,5}. (bubble sort is implemented with a flag variable)The number of
itera ons in selec on sort and bubble sort respec vely are __________
a) 5 and 4
b) 1 and 4
c) 0 and 4
d) 4 and 1
View Answer
Answer: d
Explana on: Selec on sort is insensi ve to input, hence 4(n-1) itera ons. Whereas bubble sort iterates
only once to set the flag to 0 as the input is already sorted.
Answer: d
Explana on: The best, average and worst case complexi es of selec on sort is O(n2).
(n-1) + (n-2) + (n-3) + …. + 1 = (n(n-1))/2 ~ (n2)/2.
This set of Data Structure Mul ple Choice Ques ons & Answers (MCQs) focuses on “Bubble Sort”.
Answer: a
Explana on: As the name suggests, external sor ng algorithm uses external memory like tape or disk.
Answer: b
Explana on: As the name suggests, internal sor ng algorithm uses internal main memory.
Answer: d
Explana on: Bubble sort works by star ng from the first element and swapping the elements if required
in each itera on.
adver sement
arr[k] = arr[k+1];
arr[k+1] = temp;
}
}
b)
Sanfoundry Cer fica on Contest of the Month is Live. 100+ Subjects. Par cipate Now!
arr[k] = arr[k+1];
arr[k+1] = temp;
c)
arr[k] = arr[k+1];
arr[k+1] = temp;
}
d)
arr[k] = arr[k+1];
arr[k+1] = temp;
View Answer
Answer: a
Explana on: The outer loop keeps count of number of itera ons, and the inner loop checks to see if
swapping is necessary.
Answer: d
Explana on: Bubble sort works by star ng from the first element and swapping the elements if required
in each itera on even in the average case.
6. Which of the following is not an advantage of op mised bubble sort over other sor ng techniques in
case of sorted elements?
a) It is faster
b) Consumes less memory
c) Detects whether the input is already sorted
d) Consumes less me
View Answer
Answer: c
Explana on: Op mised Bubble sort is one of the simplest sor ng techniques and perhaps the only
advantage it has over other techniques is that it can detect whether the input is already sorted. It is
faster than other in case of sorted array and consumes less me to describe whether the input array is
sorted or not. It consumes same memory than other sor ng techniques. Hence it is not an advantage.
7. The given array is arr = {1, 2, 4, 3}. Bubble sort is used to sort the array elements. How many itera ons
will be done to sort the array?
a) 4
b) 2
c) 1
d) 0
View Answer
Answer: a
Explana on: Even though the first two elements are already sorted, bubble sort needs 4 itera ons to
sort the given array.
8. How can you improve the best case efficiency in bubble sort? (The input is already sorted)
a)
swapped = true;
arr[k] = arr[k+1];
arr[k+1] = temp;
swapped = false;
}
b)
swapped = false;
arr[k] = arr[k+1];
arr[k+1] = temp;
c)
swapped = false;
arr[k] = arr[k+1];
arr[k+1] = temp;
swapped = true;
}
}
d)
arr[k] = arr[k+1];
arr[k+1] = temp;
swapped = true;
View Answer
Answer: c
Explana on: A boolean variable ‘swapped’ determines whether any swapping has happened in a
par cular itera on, if no swapping has occurred, then the given array is sorted and no more itera ons
are required.
9. What is the best case efficiency of bubble sort in the improvised version?
a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer
Answer: c
Explana on: Some itera ons can be skipped if the list is sorted, hence efficiency improves to O(n).
10. The given array is arr = {1,2,4,3}. Bubble sort is used to sort the array elements. How many itera ons
will be done to sort the array with improvised version?
a) 4
b) 2
c) 1
d) 0
View Answer
Answer: b
Explana on: Only 2 elements in the given array are not sorted, hence only 2 itera ons are required to
sort them.