This section contains carefully selected MCQs and Previous Year Questions with explanations to help students understand concepts and prepare effectively for examinations, interviews, and competitive tests.
Q: 1Which collection maintains sorted order according to natural ordering or a comparator?
Option B
In Java, a Set is a collection that does not allow duplicate elements. Among the given options, TreeSet is the collection that maintains its elements in sorted order.
By default, TreeSet uses the elements natural ordering. For example, numbers are arranged from smaller to larger, while strings are arranged according to their natural lexicographical ordering.
E.g.:
import java.util.TreeSet;
class TestExample
{
public static void main(String args[])
{
TreeSet<Integer> set = new TreeSet<Integer>();
set.add(44);
set.add(11);
set.add(33);
set.add(22);
set.add(55);
System.out.println(set);
}
}
OUTPUT
[11, 22, 33, 44, 55]
Although the values were inserted as 44, 11, 33, 22, 55, TreeSet displays them in sorted order.
Natural Ordering vs Comparator in TreeSet:
TreeSet always maintains its elements according to some ordering rule. That ordering can come either from the elements themselves (Natural Ordering) or from a separate Comparator supplied by the programmer.
| Natural Ordering | Comparator |
|---|---|
| Natural ordering is the default ordering defined by the element's class. The class generally implements the Comparable interface and defines its ordering through the compareTo() method. | A Comparator is a separate object that defines how two objects should be compared. Its ordering is supplied to the TreeSet when the set is created. |
| For example, Integer has natural ordering in which smaller numbers come before larger numbers. | For example, a Comparator can arrange integers in descending order, putting larger numbers before smaller numbers. |
| Example: TreeSet<Integer> set = new TreeSet<>(); | Example: TreeSet<Integer> set = new TreeSet<>(Comparator.reverseOrder()); |
| If we add 40, 10, 30, 20, the natural ordering produces 10, 20, 30, 40. | With a reverse-order comparator, the same values are ordered as 40, 30, 20, 10. |
| Natural ordering is defined inside the element's class through Comparable. | Comparator-based ordering is defined outside the element's class through Comparator. |
Java Collections:
| Collection / Interface | Type | Description | Order Maintained | Duplicate |
|---|---|---|---|---|
| List | Interface | Represents an ordered collection that allows elements to be accessed by position. | Insertion Order | Allowed |
| ArrayList | Class | A resizable-array implementation of List that provides fast random access to elements. | Insertion Order | Allowed |
| LinkedList | Class | A linked-list implementation of List and Deque, suitable for frequent insertion and removal operations. | Insertion Order | Allowed |
| Set | Interface | Represents a collection that stores unique elements. | Depends on Implementation | Not Allowed |
| HashSet | Class | A hash-based implementation of Set that stores unique elements without guaranteeing a particular iteration order. | No Guaranteed Order | Not Allowed |
| LinkedHashSet | Class | A Set implementation that maintains elements according to their insertion order. | Insertion Order | Not Allowed |
| SortedSet | Interface | Represents a Set whose elements are maintained according to a defined ordering. | Sorted Order | Not Allowed |
| TreeSet | Class | A sorted Set implementation that orders elements according to natural ordering or a Comparator. | Sorted Order | Not Allowed |
| Queue | Interface | Represents a collection designed primarily for holding elements before processing. | Depends on Implementation | Generally Allowed |
| PriorityQueue | Class | A queue in which elements are processed according to their priority rather than simple insertion order. | Priority order for Retrieval | Allowed |
| Map | Interface | Stores data as key-value pairs, with each key associated with at most one value. | Depends on Implementation | Keys: Not Allowed Values: Allowed |
| HashMap | Class | A hash-based implementation of Map that stores key-value pairs without guaranteeing a particular iteration order. | No Guaranteed Order | Keys: Not Allowed Values: Allowed |
| LinkedHashMap | Class | A Map implementation that maintains a predictable insertion order of entries by default. | Insertion order | Keys: Not Allowed Values: Allowed |
| TreeMap | Class | A sorted Map implementation that maintains entries according to the ordering of its keys. | Sorted Order of Keys | Keys: Not Allowed Values: Allowed |
Q: 2Which collection maintains insertion order and permits duplicate elements?
Option C
In Java, the Collections Framework provides different collection classes for storing and managing groups of objects. These classes differ in important properties such as insertion order, duplicate elements, sorting, and uniqueness.
ArrayList is a List implementation. A List generally maintains the order in which elements are inserted and allows duplicate elements.
E.g.:
import java.util.ArrayList;
class TestExample
{
public static void main(String args[])
{
ArrayList<String> list = new ArrayList<String>();
list.add("Suresh");
list.add("Banjara");
list.add("Suresh");
System.out.println(list);
}
}
OUTPUT
[Suresh, Banjara, Suresh]
The first "Suresh" and the second "Suresh" are both retained. This shows that ArrayList permits duplicates. It also preserves their insertion order.
Note:

Q: 3Which interface is the root interface of the Java Collection hierarchy for collections such as List, Set, and Queue?
Option B
In the Java Collections Framework, Collection is the main root interface for collections that store individual elements. Interfaces such as List, Set, and Queue are derived from Collection.
Collection vs Collections vs Map:
| Term | Meaning |
|---|---|
| Collection | An interface that is the main root for collections such as List, Set, and Queue. |
| Collections | A utility class containing static methods such as sort(), reverse(), shuffle(), and binarySearch(). |
| Map | An interface for storing key-value pairs. It is separate from the Collection hierarchy. |
| Iterable | A higher-level interface that allows objects to be iterated using an Iterator and is above Collection in the hierarchy. |

Q: 4Which collection class associates values with keys and orders the keys according to their natural order?
Option D
In Java, the Map interface is used when data needs to be stored as key-value pairs. Each key is associated with a value, allowing the value to be retrieved using its corresponding key.
The TreeMap class is an important implementation of the Map interface. It stores entries in a sorted order based on the natural ordering of their keys, unless a custom Comparator is supplied.
E.g.:
import java.util.TreeMap;
class Example
{
public static void main(String args[])
{
TreeMap<Integer, String> students = new TreeMap<>();
students.put(103, "Suresh");
students.put(101, "Banjara");
students.put(102, "Nirmohi");
System.out.println(students);
}
}
OUTPUT
{101=Banjara, 102=Nirmohi, 103=Suresh}
Q: 5Which Map implementation maintains insertion order?
Option C
In Java, a Map stores data in the form of key-value pairs. Different Map implementations provide different ordering behaviors.
LinkedHashMap is a Map implementation that maintains a predictable insertion order of its entries by default. This means that when entries are retrieved or iterated, they appear in the same order in which they were inserted.
E.g.:
import java.util.LinkedHashMap;
class TestExample
{
public static void main(String args[])
{
LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();
map.put(3, "Sangari");
map.put(1, "Banjara");
map.put(4, "Suresh");
map.put(2, "Nirmohi");
System.out.println(map);
}
}
OUTPUT
{3=Sangari, 1=Banjara, 4=Suresh, 2=Nirmohi}
The entries are maintained in their insertion order {3=Sangari, 1=Banjara, 4=Suresh, 2=Nirmohi}. Notice that the keys are not sorted numerically. They remain in the order 3, 1, 4, 2 because that was the order in which they were inserted.
Q: 6Which of the following is false about Vector class in Java?
Option D
In Java, a Vector is a class that implements a dynamic array, which means it can grow or shrink automatically as elements are added or removed.
Q: 7Which statement about HashMap is correct?
Option B
HashMap is a commonly used implementation of the Java Map interface. It stores data in the form of key-value pairs, where each key is unique.
One important property of HashMap is that it allows one null key and can also contain multiple null values.
E.g.:
import java.util.HashMap;
class TestExample
{
public static void main(String args[])
{
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(null, "Suresh");
map.put(1, "Banjara");
map.put(2, null);
map.put(3, null);
System.out.println(map);
}
}
OUTPUT
{null=Suresh, 1=Banjara, 2=null, 3=null}
Here, null is used as a key once, while null can appear as a value for multiple different keys. So, a HashMap can have one null key and multiple null values.
If another entry is inserted using null as the key as map.put(null, "Nirmohi"); it does not create a second null key. Instead, the value associated with the existing null key is replaced.
E.g.:
import java.util.HashMap;
class TestExample
{
public static void main(String args[])
{
HashMap<Integer, String> map = new HashMap<Integer, String>();
map.put(null, "Suresh");
map.put(1, "Banjara");
map.put(2, null);
map.put(3, null);
map.put(null, "Nirmohi");
System.out.println(map);
}
}
OUTPUT
{null=Nirmohi, 1=Banjara, 2=null, 3=null}
You have reached the end of this topic. Continue learning with the next topic below.
Thank you so much for taking the time to read my Computer Science MCQs section carefully. Your support and interest mean a lot, and I truly appreciate you being part of this journey. Stay connected for more insights and updates! If you'd like to explore more tutorials and insights, check out my YouTube channel.
Don’t forget to subscribe and stay connected for future updates.