Java Set MCQs with answers Page - 4

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 will happen if you try to add null to a TreeSet?

  • (A) It will be added
  • (B) It will be ignored
  • (C) It will throw NullPointerException
  • (D) It will be replaced

A

Admin • 828.43K Points
Coach

Q. Which of the following Set implementations is backed by a hash table?

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

A

Admin • 828.43K Points
Coach

Q. Which method is used to remove all elements from a Set?

  • (A) removeAll()
  • (B) clear()
  • (C) deleteAll()
  • (D) reset()

A

Admin • 828.43K Points
Coach

Q. Which Set implementation should be used when elements are enums?

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

A

Admin • 828.43K Points
Coach

Q. What will happen when you use the addAll() method on a Set?

  • (A) Duplicates are added
  • (B) Only new elements are added
  • (C) Existing elements are replaced
  • (D) All elements are cleared before adding

A

Admin • 828.43K Points
Coach

Q. Which collection interface does NOT allow duplicate elements?

  • (A) List
  • (B) Map
  • (C) Set
  • (D) Queue

A

Admin • 828.43K Points
Coach

Q. What is the main difference between Set and List?

  • (A) List is unordered, Set is ordered
  • (B) Set allows duplicates, List does not
  • (C) List maintains order and allows duplicates, Set does not
  • (D) There is no difference

A

Admin • 828.43K Points
Coach

Q. Which class implements a Set that uses a red-black tree for ordering?

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

A

Admin • 828.43K Points
Coach

Q. What happens when you use `equals()` to compare two Sets with same elements?

  • (A) Returns false if order differs
  • (B) Returns true only if order is same
  • (C) Returns true if contents are same
  • (D) Throws ClassCastException

A

Admin • 828.43K Points
Coach

Q. Which of the following allows only unique values and provides fast lookup?

  • (A) List
  • (B) Map
  • (C) HashSet
  • (D) ArrayList
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