NPTEL Data Structure and Algorithms using Java Week 8 Assignment Answers 2024
1. Please read the following statements and then answer the question that follows.
- All the operations learned for the HashMap class is equally applicable with TreeMap class.
- A Hamiltonian path is one in which all vertices are traversed exactly once
- Always use generics and diamond operators to declare a new map.
- keySet(): returns a Set view of the keys contained in the map.
- entrySet(): returns a Set view of the mappings contained in this map.
- The minimum spanning tree problem is related to the weighted graph, where we find a spanning tree so that the sum of all the weights of all the edges in the tree is minimum.
- The shortest path problem is the single source shortest path problem. In this problem, there is a distinct vertex, called source vertex and it requires finding the shortest path from this source vertex to all other vertices.
- A Map is a data structure that’s designed for fast lookups
- For the key-object association, a hash function is required. It is system-dependent and for the internal use of the system
- A Map is an object that maps keys to values or is a collection of key-value pairs. It models the function abstraction in mathematics.
Which of the above statement(s) is(are) true?
a. Only statements 1,2,3,4 are true
b. All statements except 1,2,3,4 are true
c. All statements are true
d. Only statements 7,8,9,10 are true
Answer :- For Answers Click Here
2. When you attempt to retrieve a value from a HashMap using a key that does not exist, which of the following statements is correct?
a. It returns null.
b. It throws a NoSuchElementException.
c. It throws a NullPointerException.
d. It’s essential to check for null to avoid potential NullPointerExceptions in your code when dealing with HashMaps.
Answer :- For Answers Click Here
3. Consider an adjacency matrix (M) representation for a simple unweighted graph having V vertices. What is the maximum number of non-zero entries possible in M?
a. VV
b. V(V-1)
c. (V(V+1))/2
d. V(V+1)
Answer :-
4. Consider a weighted, directed graph with V vertices and E edges. Which data structure is/are not typically suitable for efficiently finding the shortest paths between all pairs of vertices?
a. Adjacency matrix
b. Priority queue
c. Stack
d. Hash table
Answer :- For Answers Click Here
5. Which Java Collection is commonly used to represent a graph as an adjacency list?
a. ArrayList
b. HashMap
c. TreeMap
d. HashSet
Answer :-
6. Which of the following statement/s is/are true in the context of retrieving a value from a HashMap in Java? (assuming a good hash function and a reasonably low load factor)
a. The time complexity of retrieving a value from a HashMap O(1)
b. The time complexity of retrieving a value from a HashMap O(log n)
c. The time complexity of retrieving a value from a HashMap O(n)
d. The time to retrieve a value from HashMap does not depend on its size.
Answer :-
7. Which of the following statements about HashMap in Java is correct?
a. HashMap allows duplicate keys but not duplicate values.
b. HashMap allows duplicate values but not duplicate keys.
c. Attempting to put a duplicate key with a different value will keep the previous value associated with that key.
d. Attempting to put a duplicate key with a different value will overwrite the previous value associated with that key.
Answer :- For Answers Click Here
8. When implementing a breadth-first search (BFS) algorithm on a graph, what data structure is typically used to keep track of vertices in the order they are visited?
a. Stack
b. Queue
c. Heap
d. Linked list
Answer :-
9. In the context of data structures, what is the primary purpose of an adjacency matrix?
a. To store the values associated with vertices in the graph.
b. To efficiently represent and store the relationships between vertices in a graph.
c. To determine the degree of a vertex in the graph.
d. To find the shortest path between two vertices.
Answer :-
10. In the context of graph traversal, what is the time complexity of determining whether a graph is bipartite using depth-first search (DFS)?
a. O(V)
b. O(V x E)
c. O(V^2)
d. O(E + V)
Answer :- For Answers Click Here