Explore the essentials of the Java Collection Framework with this beginner-friendly quiz covering key interfaces, classes, methods, and properties. Ideal for interview preparation and revising core collection concepts in Java.
Which interface is considered the root of the Java Collection Framework hierarchy?
Explanation: The Collection interface is the root interface for most of the collection classes like List, Set, and Queue. List and Set are subinterfaces that extend Collection, while Map does not extend Collection and is part of a different hierarchy. Choosing List or Set would be incorrect because they are branches, not the root.
Which interface in the Java Collection Framework does NOT extend the Collection interface?
Explanation: Map is not a subtype of Collection and represents a separate hierarchy designed for key-value pairs. List, Set, and Queue all extend Collection and thus form part of the Collection hierarchy. Choosing these would not be correct as they all inherit methods from Collection.
In which Java package are most Collection Framework classes and interfaces found?
Explanation: The java.util package houses all the core collection classes and interfaces. java.lang is for fundamental classes, java.io is for input/output operations, and java.collections is not a valid Java package. Therefore, only java.util is correct.
Which Collection interface allows storage of duplicate elements?
Explanation: List allows duplicate elements as it represents a sequence where order matters. Set prohibits duplicates, Map uses unique keys (though values can repeat), and Queue allows duplicates but the primary context for this question is List. Choosing Set or Map ignores their uniqueness constraint.
Which collection implementation maintains the order in which elements are inserted?
Explanation: LinkedHashSet preserves insertion order due to its linked list implementation. HashSet and HashMap do not guarantee any order, and TreeSet maintains sorted, not insertion, order. Only LinkedHashSet ensures elements remain in the order they were added.
Which of the following is a legacy class from earlier Java versions, synchronized by default?
Explanation: Vector is a legacy class existing before the Collection Framework was introduced and is synchronized by default. ArrayList and LinkedList are part of the newer framework and are unsynchronized. 'Set' is an interface, not a class, making only Vector correct.
What method is commonly used to add an element to a Java collection?
Explanation: The 'add' method is standard for inserting elements into most collections. 'put' is used for key-value pairs in maps, while 'insert' and 'append' are not standard Collection methods. Choosing anything other than 'add' wouldn't work for typical Collection objects.
Which interface represents a collection of objects stored as key-value pairs?
Explanation: Map is specifically designed for key-value pair storage in the Java Collection Framework. List and Set store single values rather than pairs, and Collection is a broader root interface not focused on keys and values. Therefore, only Map satisfies this requirement.
Which method returns the number of elements in a Java collection?
Explanation: The correct method is 'size', which returns an integer showing how many elements a collection contains. 'length' is used for arrays, 'count' is not a standard collection method, and 'getSize' is an incorrect or non-existent method name. Thus, 'size' is correct.
Which of the following collections does NOT allow null elements?
Explanation: TreeSet does not allow nulls because it sorts elements, and null cannot be compared. ArrayList, HashSet, and LinkedList can all accept null values. Choosing any of these other options would misrepresent how null is handled within them.
Which list implementation is synchronized by default?
Explanation: Vector is synchronized by default, which means it is thread-safe for concurrent use. ArrayList and LinkedList are not synchronized, and while Stack is also synchronized, Vector is more basic and broadly represents the original synchronized implementation. Only Vector matches all traditional synchronization requirements here.
Which collection is generally considered fastest for random access of elements by index?
Explanation: ArrayList provides constant time (O(1)) random access due to its array-like structure. LinkedList requires traversal, making it slower, and Vector, while also array-based, usually has slightly more overhead. Stack is based on Vector, but ArrayList is the most widely used for random access reasons.
Which list is most efficient for frequent insertions and deletions in the middle of the list?
Explanation: LinkedList excels at frequent insertions and deletions, especially in the middle, due to its doubly-linked structure. ArrayList and Vector have to shift many elements after such operations, making them less efficient. 'Array' is not part of the Collection Framework and has fixed size.
What is the default initial capacity of an ArrayList in Java?
Explanation: The default capacity for an ArrayList, when no size is specified, is 10. The other numbers are either too small or represent common default sizes in other frameworks, not Java ArrayList. Choosing any value other than 10 would be inaccurate according to Java's documentation.
Which method adds an element at a specific index in a list?
Explanation: add(index, element) inserts the element at the specified index, shifting subsequent elements. 'set(index, element)' replaces the value at the index, while 'insert' and 'put' do not exist for lists. Therefore, only 'add(index, element)' correctly adds at a specific position.