Java Map MCQs with answers Page - 5

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. Which method in the Map interface is used to return the value if present, or else compute it?

  • (A) computeIfPresent()
  • (B) getOrDefault()
  • (C) computeIfAbsent()
  • (D) mapOrElse()

A

Admin • 831.35K Points
Coach

Q. Which Map implementation uses the least memory for enum keys?

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

A

Admin • 831.35K Points
Coach

Q. Which method is used to check if a Map is empty?

  • (A) isEmpty()
  • (B) empty()
  • (C) hasNoValues()
  • (D) checkEmpty()

A

Admin • 831.35K Points
Coach

Q. What does the `putIfAbsent()` method do?

  • (A) Adds a new entry regardless of presence
  • (B) Replaces old value
  • (C) Adds only if key is absent
  • (D) Throws an exception if key exists

A

Admin • 831.35K Points
Coach

Q. Which interface does TreeMap implement apart from Map?

  • (A) Queue
  • (B) SortedMap
  • (C) List
  • (D) SortedSet

A

Admin • 831.35K Points
Coach

Q. Which method in Map replaces all values based on a function?

  • (A) forEach()
  • (B) replaceAll()
  • (C) mapValues()
  • (D) setAll()

A

Admin • 831.35K Points
Coach

Q. How can you create an immutable Map in Java 9?

  • (A) new HashMap<>()
  • (B) Map.of()
  • (C) Collections.unmodifiableMap()
  • (D) Map.emptyMap()

A

Admin • 831.35K Points
Coach

Q. What is the time complexity for insertion in HashMap?

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

A

Admin • 831.35K Points
Coach

Q. What happens when you call clear() on a Map?

  • (A) It deletes only keys
  • (B) It deletes only values
  • (C) It deletes all entries
  • (D) Nothing happens

A

Admin • 831.35K Points
Coach

Q. Which class is best suited when Map order needs to be preserved and fast iteration is needed?

  • (A) HashMap
  • (B) TreeMap
  • (C) LinkedHashMap
  • (D) EnumMap
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