0% found this document useful (0 votes)
56 views42 pages

Data Structure

Uploaded by

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

Data Structure

Uploaded by

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

Data Structure MCQ (Mul ple Choice Ques ons)

Here are 1000 Data Structure MCQ (Chapterwise).

1. What is a data structure?


a) A programming language
b) A collec on of algorithms
c) A way to store and organize data
d) A type of computer hardware
View Answer

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.

2. What are the disadvantages of arrays?


a) Index value of an array can be nega ve
b) Elements are sequen ally accessed
c) Data structure like queue or stack cannot be implemented
d) There are chances of wastage of memory space if elements inserted in an array are lesser than the
allocated size
View Answer

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.

3. Which data structure is used for implemen ng recursion?


a) Stack
b) Queue
c) List
d) Array
View Answer

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.

5. Which of the following is not the applica on of stack?


a) Data Transfer between two asynchronous process
b) Compiler Syntax Analyzer
c) Tracking of local variables at run me
d) A parentheses balancing program
View Answer

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.

7. What is the value of the pos ix expression 6 3 2 4 + – *?


a) 74
b) -18
c) 22
d) 40
View Answer

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.

11. The prefix form of A-B/ (C * D ^ E) is?


a) -A/B*C^DE
b) -A/BC*^DE
c) -ABCD*^DE
d) -/*^ACBDE
View Answer

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.

15. What is a bit array?


a) Data structure that compactly stores bits
b) Data structure for represen ng arrays of records
c) Array in which elements are not present in con nuous loca ons
d) An array in which most of the elements have the same value
View Answer

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.

19. Which algorithm is used in the top tree data structure?


a) Backtracking
b) Divide and Conquer
c) Branch
d) Greedy
View Answer

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.

20. What is the need for a circular queue?


a) easier computa ons
b) implement LIFO principle in queues
c) effec ve usage of memory
d) to delete elements based on priority
View Answer

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.

22. Which of the following is also known as Rope data structure?


a) Linked List
b) Array
c) String
d) Cord
View Answer

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.

23. What will be the output of the following program?

main()

char str[]="san foundry";

int len = strlen(str);

int i;

for(i=0;i<len;i++)

push(str[i]); // pushes an element into stack

for(i=0;i<len;i++)

pop(); //pops an element from the stack

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).

25. What is an AVL tree?


a) a tree which is unbalanced and is a height balanced tree
b) a tree which is balanced and is a height balanced tree
c) a tree with atmost 3 children
d) a tree with three children
View Answer

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.

28. What is the use of the bin data structure?


a) to have efficient traversal
b) to have efficient region query
c) to have efficient dele on
d) to have efficient inser on
View Answer

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.

30. What is the func onality of the following piece of code?

public void display()

if(size == 0)

System.out.println("underflow");

else

Node current = first;

while(current != null)

System.out.println(current.getEle());

current = current.getNext();

}
}

a) display the list


b) reverse the list
c) reverse the list excluding top-of-the-stack-element
d) display the list excluding top-of-the-stack-element
View Answer

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.

32. What is the advantage of a hash table as a data structure?


a) easy to implement
b) faster access of data
c) exhibit good locality of reference
d) very efficient for less number of entries
View Answer

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.

33. Which type of data structure is a ternary heap?


a) Hash
b) Array
c) Priority Stack
d) Priority Queue
View Answer

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.

36. What is the output of the following Java code?

public class array

public sta c void main(String args[])

int []arr = {1,2,3,4,5};

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.

37. In simple chaining, what data structure is appropriate?


a) Doubly linked list
b) Circular linked list
c) Singly linked list
d) Binary trees
View Answer

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”.

1. Which of these best describes an array?


a) A data structure that shows a hierarchical behavior
b) Container of objects of similar types
c) Arrays are immutable once ini alised
d) Array is not a data structure
View Answer

Answer: b
Explana on: Array contains elements only of the same type.

2. How do you ini alize an array in C?


a) int arr[3] = (1,2,3);
b) int arr(3) = {1,2,3};
c) int arr[3] = {1,2,3};
d) int arr(3) = (1,2,3);
View Answer

Answer: c
Explana on: This is the syntax to ini alize an array in C.

3. How do you instan ate an array in Java?


a) int arr[] = new int(3);
b) int arr[];
c) int arr[] = new int[3];
d) int arr() = new int(3);
View Answer

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[][];

5. What is the output of the following Java code?

Subscribe Now: Data Structure Newsle er | Important Subjects Newsle ers

public class array

public sta c void main(String args[])

int []arr = {1,2,3,4,5};

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.

6. What is the output of the following Java code?

public class array

public sta c void main(String args[])

int []arr = {1,2,3,4,5};

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.

7. When does the ArrayIndexOutOfBoundsExcep on occur?


a) Compile- me
b) Run- me
c) Not an error
d) Not an excep on at all
View Answer

Answer: b
Explana on: ArrayIndexOutOfBoundsExcep on is a run- me excep on and the compila on is error-free.

8. Which of the following concepts make extensive use of arrays?


a) Binary trees
b) Scheduling of processes
c) Caching
d) Spa al locality
View Answer

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.

9. What are the advantages of arrays?


a) Objects of mixed data types can be stored
b) Elements in an array cannot be sorted
c) Index of first element of an array is 1
d) Easier to store elements of same data type
View Answer

Answer: d
Explana on: Arrays store elements of the same data type and present in con nuous memory loca ons.

10. What are the disadvantages of arrays?


a) Data structure like queue or stack cannot be implemented
b) There are chances of wastage of memory space if elements inserted in an array are lesser than the
allocated size
c) Index value of an array can be nega ve
d) Elements are sequen ally accessed
View Answer

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.

11. Assuming int is of 4bytes, what is the size of int arr[15];?


a) 15
b) 19
c) 11
d) 60
View Answer

Answer: d
Explana on: Since there are 15 int elements and each int is of 4bytes, we get 15*4 = 60bytes.

12. In general, the index of the first element in an array is __________


a) 0
b) -1
c) 2
d) 1
View Answer

Answer: a
Explana on: In general, Array Indexing starts from 0. Thus, the index of the first element in an array is 0.

13. Elements in an array are accessed _____________


a) randomly
b) sequen ally
c) exponen ally
d) logarithmically
View Answer

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”.

1. Which of the following searching algorithm is fastest?


a) binary search
b) linear search
c) jump search
d) all are equally fast
View Answer
Answer: a
Explana on: Binary search has the least me complexity (equal to log n) out of the given searching
algorithms. This makes binary search preferable in most cases.

2. Where is linear searching used?


a) Used all the me
b) When the list has only a few elements
c) When performing a single search in an unordered list
d) When the list has only a few elements and When performing a single search in an unordered list
View Answer

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.

3. How can Jump Search be improved?


a) Step size should be other than sqrt(n)
b) Cannot be improved
c) Begin from the kth item, where k is the step size
d) Start searching from the end
View Answer

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.

8. Interpola on search is a varia on of?


a) Exponen al search
b) Linear search
c) Binary search
d) Jump search
View Answer

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.

9. Which of the following is not an applica on of binary search?


a) To search in unordered list
b) Debugging
c) Union of intervals
d) To find the lower/upper bound in an ordered sequence
View Answer
Answer: a
Explana on: In Binary search, the elements in the list should be sorted. It is applicable only for ordered
list. Hence Binary search in unordered list is not an applica on.

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.

11. Which of the following is not an advantage of Fibonacci Search?


a) When the element being searched for has a non uniform access storage
b) It can be applied efficiently on unsorted arrays
c) Can be used for large arrays which do not fit in the CPU cache or in the RAM
d) Can be used in magne c tapes
View Answer

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).

3. What is the worst case run me of linear search(recursive) algorithm?


a) O(n)
b) O(logn)
c) O(n2)
d) O(nx)
View Answer

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

4. Linear search(recursive) algorithm used in _____________


a) When the size of the dataset is low
b) When the size of the dataset is large
c) When the dataset is unordered
d) Never used
View Answer

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.

8. Which of the following code snippet performs linear search recursively?


a)

for(i=0;i<n;i++)

if(a[i]==key)

prin ("element found");

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.

2. What is the advantage of selec on sort over other sor ng techniques?


a) It is faster than any other sor ng technique
b) It is scalable
c) It works best for inputs which are already sorted
d) It requires no addi onal storage space
View Answer

Answer: d
Explana on: Since selec on sort is an in-place sor ng algorithm, it does not require addi onal storage.

3. Which of the following method is used for sor ng in merge sort?


a) par oning
b) merging
c) exchanging
d) selec on
View Answer

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

4. Which of the following sor ng algorithm does not use recursion?


a) bo om up merge sort
b) merge sort
c) heap sort
d) quick sort
View Answer

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.

6. Which of the following sor ng algorithms is the fastest?


a) Merge sort
b) Shell sort
c) Inser on sort
d) Quick sort
View Answer

Answer: d
Explana on: Quick sort is the fastest known sor ng algorithm because of its highly op mized inner loop.

7. Shell sort algorithm is an example of?


a) Bo om-up sor ng
b) In-place sor ng
c) Internal sor ng
d) External sor ng
View Answer

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.

10. Which of the following sor ng algorithm is used by C++ internally?


a) quicksort
b) merge sort
c) introsort
d) heap sort
View Answer
Answer: c
Explana on: Introsort is the in built sor ng algorithm used by C++. It is an example of a hybrid sor ng
algorithm which means it uses more than one sor ng algorithm as a rou ne.

11. Which of the following sor ng algorithm is stable?


a) Introsort
b) Tim sort
c) Heap sort
d) Quick sort
View Answer

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.

13. Which of the following pair of sor ng algorithms are stable?


a) gnome sort and merge sort
b) heap sort and merge sort
c) gnome sort and quick sort
d) merge sort and selec on sort
View Answer

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”.

1. How many passes does an inser on sort algorithm consist of?


a) N
b) N-1
c) N+1
d) N2
View Answer
Answer: b
Explana on: An inser on algorithm consists of N-1 passes when an array of N elements is given.

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.

3. What is the average case running me of an inser on sort algorithm?


a) O(N)
b) O(N log N)
c) O(log N)
d) O(N2)
View Answer

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.

5. What is the average number of inversions in an array of N dis nct numbers?


a) N(N-1)/4
b) N(N+1)/2
c) N(N-1)/2
d) N(N-1)/3
View Answer

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.

Note: Join free Sanfoundry classes at Telegram or Youtube


6. What is the running me of an inser on sort algorithm if the input is pre-sorted?
a) O(N2)
b) O(N log N)
c) O(N)
d) O(M log N)
View Answer

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.

9. Which of the following real me examples is based on inser on sort?


a) arranging a pack of playing cards
b) database scenarios and distributes scenarios
c) arranging books on a library shelf
d) real- me systems
View Answer

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”.

1. Which of the following is correct with regard to inser on sort?


a) inser on sort is stable and it sorts In-place
b) inser on sort is unstable and it sorts In-place
c) inser on sort is stable and it does not sort In-place
d) inser on sort is unstable and it does not sort In-place
View Answer

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

4. Inser on sort is an example of an incremental algorithm.


a) True
b) False
View Answer

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.

5. Consider the code given below, which runs inser on sort:

Note: Join free Sanfoundry classes at Telegram or Youtube

void inser onSort(int arr[], int array_size)

int i, j, value;

for (i = 1; i < array_size; i++)

value = arr[i];

j = i;

while (________ )

arr[j] = arr[j − 1];

j = j − 1;

arr[j] = value;

Which condi on will correctly implement the while loop?


a) (j > 0) || (arr[j − 1] > value)
b) (j > 0) && (arr[j − 1] > value)
c) (j > 0) && (arr[j + 1] > value)
d) (j > 0) && (arr[j + 1] < value)
View Answer

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.

10. Which of the following is not an exchange sort?


a) Bubble Sort
b) Quick Sort
c) Par on-exchange Sort
d) Inser on Sort
View Answer

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”.

1. What is an in-place sor ng algorithm?


a) It needs O(1) or O(logn) memory to create auxiliary loca ons
b) The input is already sorted and in-place
c) It requires addi onal storage
d) It requires addi onal space
View Answer

Answer: a
Explana on: Auxiliary memory is required for storing the data temporarily.

2. In the following scenarios, when will you use selec on sort?


a) The input is already sorted
b) A large file has to be sorted
c) Large values need to be sorted with small keys
d) Small values need to be sorted with large keys
View Answer
Answer: c
Explana on: Selec on is based on keys, hence a file with large values and small keys can be efficiently
sorted with selec on sort.

3. What is the worst case complexity of selec on sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer

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

4. Select the appropriate code that performs selec on sort.


a)

int min;

for(int j=0; j<arr.length-1; j++)

min = j;

for(int k=j+1; k<=arr.length-1; k++)

if(arr[k] < arr[min])

min = k;

int temp = arr[min];

arr[min] = arr[j];

arr[j] = temp;

b)

Subscribe Now: Design and Analysis of Algorithms Newsle er | Important Subjects Newsle ers

int min;

for(int j=0; j<arr.length-1; j++)


{

min = j;

for(int k=j+1; k<=arr.length; k++)

if(arr[k] < arr[min])

min = k;

int temp = arr[min];

arr[min] = arr[j];

arr[j] = temp;

c)

int min;

for(int j=0; j<arr.length-1; j++)

min = j;

for(int k=j+1; k<=arr.length-1; k++)

if(arr[k] > arr[min])

min = k;

int temp = arr[min];

arr[min] = arr[j];

arr[j] = temp;

d)

int min;

for(int j=0; j<arr.length-1; j++)

{
min = j;

for(int k=j+1; k<=arr.length; k++)

if(arr[k] > arr[min])

min = k;

int temp = arr[min];

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.

5. What is the advantage of selec on sort over other sor ng techniques?


a) It requires no addi onal storage space
b) It is scalable
c) It works best for inputs which are already sorted
d) It is faster than any other sor ng technique
View Answer

Answer: a
Explana on: Since selec on sort is an in-place sor ng algorithm, it does not require addi onal storage.

6. What is the average case complexity of selec on sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer

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.

7. What is the disadvantage of selec on sort?


a) It requires auxiliary memory
b) It is not scalable
c) It can be used for small keys
d) It takes linear me to sort the elements
View Answer

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.

10. What is the best case complexity of selec on sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer

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”.

1. What is an external sor ng algorithm?


a) Algorithm that uses tape or disk during the sort
b) Algorithm that uses main memory during the sort
c) Algorithm that involves swapping
d) Algorithm that are considered ‘in place’
View Answer

Answer: a
Explana on: As the name suggests, external sor ng algorithm uses external memory like tape or disk.

2. What is an internal sor ng algorithm?


a) Algorithm that uses tape or disk during the sort
b) Algorithm that uses main memory during the sort
c) Algorithm that involves swapping
d) Algorithm that are considered ‘in place’
View Answer

Answer: b
Explana on: As the name suggests, internal sor ng algorithm uses internal main memory.

3. What is the worst case complexity of bubble sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer

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

4. Select the appropriate code that performs bubble sort.


a)

for(int j=arr.length-1; j>=0; j--)

for(int k=0; k<j; k++)

if(arr[k] > arr[k+1])

int temp = arr[k];

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!

for(int j=arr.length-1; j>=0; j--)

for(int k=0; k<j; k++)

if(arr[k] < arr[k+1])

int temp = arr[k];

arr[k] = arr[k+1];

arr[k+1] = temp;

c)

for(int j=arr.length; j>=0; j--)

for(int k=0; k<j; k++)

if(arr[k] > arr[k+1])

int temp = arr[k];

arr[k] = arr[k+1];

arr[k+1] = temp;

}
d)

for(int j=arr.length; j>=0; j--)

for(int k=0; k<j; k++)

if(arr[k] > arr[k+2])

int temp = arr[k];

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.

5. What is the average case complexity of bubble sort?


a) O(nlogn)
b) O(logn)
c) O(n)
d) O(n2)
View Answer

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)

boolean swapped = false;

for(int j=arr.length-1; j>=0 && swapped; j--)

swapped = true;

for(int k=0; k<j; k++)

if(arr[k] > arr[k+1])

int temp = arr[k];

arr[k] = arr[k+1];

arr[k+1] = temp;

swapped = false;

}
b)

boolean swapped = true;

for(int j=arr.length-1; j>=0 && swapped; j--)

swapped = false;

for(int k=0; k<j; k++)

if(arr[k] > arr[k+1])

int temp = arr[k];

arr[k] = arr[k+1];

arr[k+1] = temp;

c)

boolean swapped = true;

for(int j=arr.length-1; j>=0 && swapped; j--)

swapped = false;

for(int k=0; k<j; k++)

if(arr[k] > arr[k+1])

int temp = arr[k];

arr[k] = arr[k+1];

arr[k+1] = temp;

swapped = true;

}
}

d)

boolean swapped = true;

for(int j=arr.length-1; j>=0 && swapped; j--)

for(int k=0; k<j; k++)

if(arr[k] > arr[k+1])

int temp = arr[k];

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.

You might also like