What is a TreeSet in Java?

Java MCQ: What is a TreeSet in Java?

a) A set that stores elements in a sorted order
b) A set that allows duplicates
c) A set that stores key-value pairs
d) A set that does not allow null elements

Answer:

a) A set that stores elements in a sorted order

Explanation:

A TreeSet in Java is a set that stores elements in a sorted order, determined either by their natural ordering or by a comparator provided at set creation. It does not allow duplicate elements.

Understanding TreeSet is essential for managing collections where sorted order is required in addition to uniqueness.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top