Java Map MCQs with answers Page - 3

You will find multiple-choice questions (MCQs) related to #Java Map here. Go through these questions to prepare effectively for your upcoming exams and interviews.

To view the correct answer for any question, simply click the "Show Answer" button.

Have a question to share? Click on "Add Question" to contribute!

A

Admin • 831.35K Points
Coach

Q. What is the output of `map.put(1, null)` if 1 already exists with a non-null value?

  • (A) The value is not updated
  • (B) The key is removed
  • (C) The value is replaced with null
  • (D) Throws NullPointerException

A

Admin • 831.35K Points
Coach

Q. Which method returns a collection view of the values contained in the map?

  • (A) values()
  • (B) getValues()
  • (C) valueSet()
  • (D) allValues()

A

Admin • 831.35K Points
Coach

Q. Which Map method is used to perform a specific action for each entry?

  • (A) forEach()
  • (B) loop()
  • (C) traverse()
  • (D) mapEach()

A

Admin • 831.35K Points
Coach

Q. Which of the following will throw a NullPointerException in TreeMap?

  • (A) Inserting null key
  • (B) Inserting null value
  • (C) Using containsKey() on null
  • (D) All of the above

A

Admin • 831.35K Points
Coach

Q. Which method merges a new value into the Map based on the key and a remapping function?

  • (A) put()
  • (B) merge()
  • (C) join()
  • (D) combine()

A

Admin • 831.35K Points
Coach

Q. What is returned by the `remove(key)` method?

  • (A) The key
  • (B) The value removed
  • (C) True if removed
  • (D) Nothing

A

Admin • 831.35K Points
Coach

Q. Which of the following guarantees no duplicate keys and unsorted entries?

  • (A) TreeMap
  • (B) HashMap
  • (C) LinkedHashMap
  • (D) EnumMap

A

Admin • 831.35K Points
Coach

Q. What is the time complexity of get() in HashMap in average case?

  • (A) O(log n)
  • (B) O(n)
  • (C) O(1)
  • (D) O(n log n)

A

Admin • 831.35K Points
Coach

Q. Which Map implementation uses red-black tree internally?

  • (A) HashMap
  • (B) TreeMap
  • (C) Hashtable
  • (D) EnumMap

A

Admin • 831.35K Points
Coach

Q. Which method replaces the value for a key only if it is already mapped?

  • (A) putIfAbsent()
  • (B) replace()
  • (C) updateIfPresent()
  • (D) setIfPresent()
What's Tag

As you may know, questions are organized under broad categories. Each category can include various types of questions. For example, the "History" category might contain questions about the Revolt of 1857, Shivaji Maharaj, Ancient History, Buddhism, and more.

To further refine this organization, we've introduced tags, which act as sub-categories to group questions more specifically.

Verified users can add tags to any question. If you have any suggestions regarding this system, we'd love to hear from you. Contact Us

Learn More