Java Set MCQs with answers Page - 9

You will find multiple-choice questions (MCQs) related to #Java Set 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 • 828.43K Points
Coach

Q. What happens if we call `remove(null)` on a HashSet that doesn't contain null?

  • (A) Throws NullPointerException
  • (B) Throws IllegalArgumentException
  • (C) Returns false
  • (D) Removes all elements

A

Admin • 828.43K Points
Coach

Q. Which of the following is TRUE about EnumSet?

  • (A) It can store null elements
  • (B) It stores values in random order
  • (C) It is not type-safe
  • (D) It stores values in natural order of enum constants

A

Admin • 828.43K Points
Coach

Q. What will be the output of `new TreeSet<>(Arrays.asList(5, 3, 8, 1))`?

  • (A) [5, 3, 8, 1]
  • (B) [1, 3, 5, 8]
  • (C) [8, 5, 3, 1]
  • (D) Compilation error

A

Admin • 828.43K Points
Coach

Q. What happens when we add duplicate elements in LinkedHashSet?

  • (A) They are stored in order
  • (B) They are added at the end
  • (C) Only first occurrence is stored
  • (D) An exception is thrown

A

Admin • 828.43K Points
Coach

Q. Which method retrieves but does not remove the first element of a NavigableSet?

  • (A) pollFirst()
  • (B) first()
  • (C) getFirst()
  • (D) peekFirst()

A

Admin • 828.43K Points
Coach

Q. Which of the following allows concurrent access and sorted elements?

  • (A) HashSet
  • (B) TreeSet
  • (C) ConcurrentSkipListSet
  • (D) EnumSet

A

Admin • 828.43K Points
Coach

Q. What is the default capacity of a HashSet?

  • (A) 8
  • (B) 16
  • (C) 10
  • (D) 0

A

Admin • 828.43K Points
Coach

Q. How many null values can a TreeSet hold?

  • (A) One
  • (B) Unlimited
  • (C) None
  • (D) Two

A

Admin • 828.43K Points
Coach

Q. What is the output of `new HashSet<>(Arrays.asList(null, null, null))`?

  • (A) [null, null, null]
  • (B) [null]
  • (C) []
  • (D) Compilation error

A

Admin • 828.43K Points
Coach

Q. Which of the following is used to convert Set to List?

  • (A) List.of(set)
  • (B) Arrays.asList(set)
  • (C) new ArrayList<>(set)
  • (D) Set.toList()
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