Java Set MCQs with answers Page - 5

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. Which method is used to find if two sets have any common elements?

  • (A) retainAll()
  • (B) removeAll()
  • (C) containsAll()
  • (D) intersect()

A

Admin • 828.43K Points
Coach

Q. Which data structure does TreeSet use internally?

  • (A) Hashtable
  • (B) LinkedList
  • (C) Array
  • (D) Red-Black Tree

A

Admin • 828.43K Points
Coach

Q. Which one of the following Set types is backed by a hash table and allows one null element?

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

A

Admin • 828.43K Points
Coach

Q. What is the time complexity of contains() in HashSet in average case?

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

A

Admin • 828.43K Points
Coach

Q. Which of the following will throw NullPointerException?

  • (A) new HashSet<>().add(null)
  • (B) new TreeSet<>().add(null)
  • (C) new LinkedHashSet<>().add(null)
  • (D) new ArrayList<>().add(null)

A

Admin • 828.43K Points
Coach

Q. Which of these interfaces must be implemented to sort custom objects in TreeSet?

  • (A) Serializable
  • (B) Comparator or Comparable
  • (C) Cloneable
  • (D) Iterable

A

Admin • 828.43K Points
Coach

Q. Which collection type would you use to remove duplicates from a list?

  • (A) ArrayList
  • (B) LinkedList
  • (C) HashSet
  • (D) Vector

A

Admin • 828.43K Points
Coach

Q. Which of the following statements is FALSE about Set?

  • (A) Set does not allow duplicate elements
  • (B) Set allows access by index
  • (C) Set is unordered by default
  • (D) Set can store null depending on implementation

A

Admin • 828.43K Points
Coach

Q. Which implementation would you choose for a Set where order matters and duplicates are not allowed?

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

A

Admin • 828.43K Points
Coach

Q. Which of these Set types maintains elements in natural sorted order?

  • (A) LinkedHashSet
  • (B) HashSet
  • (C) TreeSet
  • (D) EnumSet
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