0% found this document useful (0 votes)
30 views

MCQs Java

This document contains multiple choice questions about data structures and algorithms including hash tables, heaps, greedy algorithms, and dynamic programming. It tests knowledge of time complexities for operations like insertion, search and deletion on different data structures as well as properties of algorithms like greedy algorithms and dynamic programming.

Uploaded by

kewal22350
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

MCQs Java

This document contains multiple choice questions about data structures and algorithms including hash tables, heaps, greedy algorithms, and dynamic programming. It tests knowledge of time complexities for operations like insertion, search and deletion on different data structures as well as properties of algorithms like greedy algorithms and dynamic programming.

Uploaded by

kewal22350
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1 Mark Questions MCQs

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

2. How is the index for an element in a hash table determined?


a) By using the element's value as the index.
b) By using the element's key as the index.
c) By using the element's value modulo the size of the hash table as the index.
d) By using the element's key modulo the size of the hash table as the index.
Answer: d

3. What is a collision in a hash table?


a) When two elements have the same key.
b) When two elements have the same value.
c) When two elements hash to the same index.
d) When an element is inserted into a full hash table.
Answer: c

4. How can collisions be resolved in a hash table?


a) By using a linked list at each index to store all elements that hash to that index.
b) By rehashing the elements until they hash to a different index.
c) By increasing the size of the hash table and reinserting all elements.
d) By using a binary search tree at each index to store all elements that hash to that index.
Answer: a or d (Both options are correct.)

5. What is the time complexity of inserting an element into a hash table?


a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: a

6. What is the time complexity of searching for an element in a hash table?


a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: a

7. What is the time complexity of deleting an element from a hash table?


a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: a

8. What is the time complexity of inserting an element into a sorted array?


a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: d

9. What is the time complexity of searching for an element in a sorted array?


a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: c

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

12. What is the time complexity of inserting an element into a heap?


a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: c

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

15. What is a priority queue?


a) A queue in which elements are ordered according to their priority.
b) A queue in which elements are ordered according to the order in which they were added.
c) A stack in which elements are ordered according to their priority.
d) A stack in which elements are ordered according to the order in which they were added.
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

22. What is the time complexity of a greedy algorithm?


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

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

24. What is the greedy choice property?


a) The property that a globally optimal solution can be arrived at by making locally optimal choices.
b) The property that a locally optimal solution can be arrived at by making globally optimal
choices.
c) The property that a random choice leads to a globally optimal solution.
d) The property that a globally optimal solution can be arrived at by making random choices.
Answer: a

25. What is the optimal substructure property?


a) The property that a globally optimal solution can be arrived at by making locally optimal choices.
b) The property that a globally optimal solution can be decomposed into subproblems that can be
solved independently.
c) The property that a locally optimal solution can be arrived at by making globally optimal choices.
d) The property that a globally optimal solution can be arrived at by making random choices.
Answer: b

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

29. Which of the following is true about greedy algorithms?


a) They always find the optimal solution.
b) They are faster than dynamic programming algorithms.
c) They are slower than dynamic programming algorithms.
d) They are applicable to a wider range of problems than dynamic programming algorithms.
Answer: b

Dynamic Programming :

30. What is dynamic programming?


a) A method of solving problems by breaking them down into smaller subproblems and storing the
solutions to these subproblems in a table.
b) A method of solving problems by dividing them into smaller subproblems and solving these
subproblems independently.
c) A method of solving problems by guessing and checking different solutions.
d) A method of solving problems by starting with the solution and working backwards to find the
problem.
Answer: a

31. What is the time complexity of a dynamic programming algorithm?


a) O(1)
b) O(n)
c) O(log n)
d) O(n log n)
Answer: b or c

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

33. What is the optimal substructure property?


a) The property that a globally optimal solution can be arrived at by making locally optimal choices.
b) The property that a globally optimal solution can be decomposed into subproblems that can be
solved independently.
c) The property that a locally optimal solution can be arrived at by making globally optimal choices.
d) The property that a globally optimal solution can be arrived at by making random choices.
Answer: b

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

35. What is memoization?


a) A technique for improving the time complexity of dynamic programming algorithms by storing
the solutions to subproblems in a table.
b) A technique for improving the space complexity of dynamic programming algorithms by storing
the solutions to subproblems in a table.
c) A technique for improving the time complexity of dynamic programming algorithms by using
recursion.
d) A technique for improving the space complexity of dynamic programming algorithms by using
recursion.
Answer: a

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

38. Dynamic programming divides problems into a number of


a) conflicting objective functions.
b) decision stages.
c) unrelated constraints.
d) policies.
e) none of the above
Answer: b

39. In dynamic programming, the output to stage n become the input to


a) stage n-1.
b) stage n itself.
c) stage n+1.
d) stage n-2.
e) none of the above
Answer : a
40. Which of the following statements are true?
Where n is the number of items and W is the weight of the Knapsack .
1. Time complexity of Knapsack is O(n* W) where W is the weight of the Knapsack and
there are n items.
2. Time complexity of Knapsack is min( O(n*W) , O(2^n) ) where W is the weight of the
Knapsack and there are n items.
3. Knapsack can be implemented in O(n*W) space .
4. Knapsack can be implemented in O(W) space.

a. 1
b. 1,2
c. 2,3,4,
d. 4
Answer: c

You might also like