MCQs Java
MCQs Java
1. A Hash Table is
a) A data structure that stores key-value pairs and allows for fast insertion, deletion, and retrieval of
data.
b) A data structure that stores data in a sorted manner and allows for fast insertion and retrieval of
data, but not deletion.
c) A data structure that stores data in a tree structure and allows for fast insertion, deletion, and
retrieval of data.
d) A data structure that stores data in a linear manner and allows for fast insertion and retrieval of
data, but not deletion.
Answer: a
10. What is the time complexity of deleting an element from a sorted array?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: b
HEAP :
11. What is a heap?
a) A complete binary tree in which the value of each node is greater than or equal to the values of its
children.
b) A complete binary tree in which the value of each node is less than or equal to the values of its
children.
c) A binary tree in which the value of each node is greater than or equal to the values of its children,
but the tree is not necessarily complete.
d) A binary tree in which the value of each node is less than or equal to the values of its children,
but the tree is not necessarily complete.
Answer: a and b (Both options are correct. A heap can be either a max heap or a min heap.)
13. What is the time complexity of extracting the maximum element from a max heap?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: a
14. What is the time complexity of extracting the minimum element from a min heap?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: a
16. What is the time complexity of inserting an element into a priority queue implemented as a
heap?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: c
17. What is the time complexity of extracting the highest priority element from a priority queue
implemented as a heap?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: a
18. What is the time complexity of inserting an element into a priority queue implemented as a
sorted array?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: d
19. What is the time complexity of extracting the highest priority element from a priority queue
implemented as a sorted array?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: b
20. What is the time complexity of inserting an element into a priority queue implemented as a
linked list?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: a
Greedy Algorithms :
21. What is a greedy algorithm?
a) An algorithm that makes the locally optimal choice at each step with the hope of finding a global
optimum.
b) An algorithm that makes the globally optimal choice at each step with the hope of finding a local
optimum.
c) An algorithm that makes random choices at each step with the hope of finding a global optimum.
d) An algorithm that makes the globally optimal choice at each step, but does not always find the
best solution.
Answer: a
Answer: b and c
23. Which of the following is an example of a problem that can be solved using a greedy algorithm?
a) Finding the shortest path between two nodes in a graph
b) Sorting a list of numbers in ascending order
c) Finding the maximum sum subarray in an array
d) Determining the minimum number of coins needed to make a certain amount of change
Answer: d
26. Which of the following is an example of a problem that exhibits both the greedy choice property
and the optimal substructure property?
a) Finding the shortest path between two nodes in a graph
b) Sorting a list of numbers in ascending order
c) Finding the maximum sum subarray in an array
d) Determining the minimum number of coins needed to make a certain amount of change
Answer: d
27. What is the time complexity of the greedy algorithm for the minimum coin change problem?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: b
28. What is the time complexity of the dynamic programming algorithm for the minimum coin
change problem?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: b
Dynamic Programming :
32. Which of the following is an example of a problem that can be solved using dynamic
programming?
a) Finding the shortest path between two nodes in a graph
b) Sorting a list of numbers in ascending order
c) Finding the maximum sum subarray in an array
d) Determining the minimum number of coins needed to make a certain amount of change
Answer: a and d
34. Which of the following is an example of a problem that exhibits the optimal substructure
property?
a) Finding the shortest path between two nodes in a graph
b) Sorting a list of numbers in ascending order
c) Finding the maximum sum subarray in an array
d) Determining the minimum number of coins needed to make a certain amount of change
Answer: a and d
36. What is the time complexity of the dynamic programming algorithm for the shortest path
problem?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: b and c
37. What is the time complexity of the Dijkstra's algorithm for the shortest path problem?
a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: c
a. 1
b. 1,2
c. 2,3,4,
d. 4
Answer: c