JAVA 1.Which of the following is true about a HashMap? 2.What can we say about t
ID: 3573223 • Letter: J
Question
JAVA
1.Which of the following is true about a HashMap?
2.What can we say about the order in which entries are stored in a HashMap?
3.Assume that the following code has been executed (same for Questions 17 thru 20):
What value is returned by the following method call?
places.size()
4.Assume that the following code has been executed (same for Questions 17 thru 20):
What value is returned by the following method call?
places.get( "south" )
5.Assume that the following code has been executed (same for Questions 17 thru 20):
What value is returned by the following method call?
places.get( "east" )
6.Assume that the following code has been executed (same for Questions 17 thru 20):
What value is returned by the following method call?
places.get( "north" )
Explanation / Answer
1. (D)All the above
HashMap is best thought as a look up Table because we can look up whether the the keys and value are present or not.
HashMap is an object , since we declare map as
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
Duplicate keys are not allowed in a HashMap.
2. (A) HashMap is an unordered data structure, there is no concept of order . Suppose we added (1==2 , 4==6 , 9==5) key-value pair to the map. When we iterate throight the Map we might not the same order , we might get as
(, 4==6 , 1==2 , 9==5), So we can say there is no concept of order at all for entries in a HashMap. If you iterate throught the keyset, the order in which keys are present are unpredictable.
3. (C)places.size() is 4 because we are adding 5 keys out of them 4 are unique and one is duplicate ("east"). Hence the size is 4.
4. (C) When we do get("south") ., It checks in the lookup table and sees that Point (0,-100) is added , Hence we get a point object at (0.0, -100.0).
5. (B) When we do get("east") ., It checks in the lookup table and sees that Point (100,0) is added at first time for east, Hence we get a point object at (100.0, 0). Why we dont get {200, 0} because east is the duplicate key hence it is not added to the HashMap.
6(C) null.Since we dont add any key as "north", so during the get operation it checks the lookup table and returns null because north is not added
Thanks, I hope it clarifies . let me know if there is anything.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.