Java collections are fundamental in Java programming, providing a wide range of data structures and algorithms to efficiently store and manipulate data.
Java everything about Java 8 features: Java 8 Tutorial and Examples
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills
The answer and explanation have been given for each MCQ.YouTube Video - Java Collections Quiz
Subscribe to my YouTube channel to learn more: https://www.youtube.com/@javaguides.
1. Which of the following is a part of the Java Collections Framework?
Answer:
Explanation:
ArrayList is a part of the Java Collections Framework, whereas Array and int[] are native array structures, and String is a final class representing strings.
2. Which is the root interface of the Java Collection framework hierarchy?
Answer:
Explanation:
In the Java Collections framework, the Collection interface is the root interface of the hierarchy. It provides the foundation upon which the List, Set, and Queue interfaces are built. On the other hand, Collections is a utility class that provides static methods to operate on or return collections. The options List/Set and Root are not the root interfaces of the Java Collection framework hierarchy.
3. Which collection does not allow duplicate elements?
Answer:
Explanation:
The Set interface does not allow duplicate elements. The List allows duplicates, and Map deals with key-value pairs where keys cannot be duplicate.
4. Which collection allows you to retrieve elements in the order they were inserted?
Answer:
Explanation:
LinkedHashMap maintains the insertion order.
5. What is the implementation of the List interface?
Answer:
Explanation:
c. LinkedList
6. What are the implementation classes of the List interface?
Answer:
Explanation:
ArrayList, LinkedList, and Vector are implementation classes of the List interface.
7. Which class is a resizable array implementation?
Answer:
Explanation:
ArrayList is essentially a dynamic array that can grow as needed.
8. Which collection ensures that elements are processed in first-in-first-out (FIFO) order?
Answer:
Explanation:
LinkedList, when used as a queue, ensures FIFO ordering.
9. Which of the following collections is sorted by its natural ordering?
Answer:
Explanation:
TreeMap stores its elements in a red-black tree structure, sorting them based on their natural ordering or by a comparator provided during creation.
10. Which collection class is synchronized?
Answer:
Explanation:
Hashtable is synchronized, meaning it's thread-safe. Other collections like HashMap are not synchronized by default.
11. Which interface provides methods to insert, remove, and inspect elements?
Answer:
Explanation:
The Queue interface provides methods like offer, poll, and peek for such operations.
12. Which of these is not a method in the Collection interface?
Answer:
Explanation:
The get() method is from the List interface, not the general Collection interface.
13. Which of the following collections is thread-safe?
Answer:
Explanation:
Vector is synchronized, meaning it's thread-safe. The other collections listed are not synchronized by default.
14. Which method removes all elements from a collection?
Answer:
Explanation:
The clear() method is used to remove all elements from a collection.
15. What are the implementation classes of the Set interface?
Answer:
Explanation:
HashSet, LinkedHashSet, and TreeSet implementation classes of the Set interface.
16. In which collection are elements stored as key-value pairs?
Answer:
Explanation:
The Map interface stores elements as key-value pairs.
17. Which of the following lists can contain the same element multiple times?
Answer:
Explanation:
A List can contain duplicate elements.
18. Which method is used to check if a collection contains a certain element?
Answer:
Explanation:
The contains() method is used to check if an element is present in a collection.
19. Which class provides a thread-safe implementation of the List?
Answer:
Explanation:
CopyOnWriteArrayList is a thread-safe variant of ArrayList in which all modifications are implemented by creating a separate copy of the underlying array.
20. Which collection provides a guaranteed constant time for basic operations like add, remove, and contains?
Answer:
Explanation:
HashSet offers constant-time performance for the basic operations assuming the hash function disperses the elements properly among the buckets.
21. Which method retrieves, but does not remove, the head (first element) of a Queue?
Answer:
Explanation:
The peek() method retrieves the element at the head of the queue without removing it.
22. Which of the following cannot contain a null value?
Answer:
Explanation:
TreeMap does not allow null keys because of ordering. The others can have at least one null key and multiple null values.
23. Which collection is synchronized and does not allow null values?
Answer:
Explanation:
Hashtable is synchronized and does not allow null keys or values.
24. Which interface represents a double-ended queue?
Answer:
Explanation:
The Deque interface represents a double-ended queue where you can insert and remove elements from both ends.
25. Which method is used to obtain the size of a collection?
Answer:
Explanation:
The size() method returns the number of elements in a collection.
26. Which of the following is ordered and sorted by its natural ordering or by a comparator?
Answer:
Explanation:
TreeSet is a sorted set and orders its elements based on their natural ordering or by a comparator provided at set creation time.
27. Which list method replaces the element at a specified position?
Answer:
Explanation:
The set() method replaces the element at the specified position in the list.
28. Which of these is not a direct implementation of the Map interface?
Answer:
Explanation:
HashSet is an implementation of the Set interface, not Map.
29. What are the implementation classes of the Map interface?
Answer:
Explanation:
HashMap, LinkedHashMap, and TreeMap are implementation classes of the Map interface.
30. HashSet internally uses?
Answer:
Explanation:
Internally, HashSet uses HashMap to store its elements. The elements you add to the HashSet become the keys in the underlying HashMap, with a constant dummy value associated with every key. This allows HashSet to leverage the fast key lookup capabilities of HashMap to provide quick implementations of its own add, remove, and contains methods.
31. Which data structure ArrayList internally uses?
Answer:
Explanation:
ArrayList internally uses a dynamic array to store its elements. As the list grows, and the capacity of the underlying array is exceeded, ArrayList creates a new, larger array and copies the old elements to it. This provides efficient random access but can make the insertion and deletion of elements in the middle of the list more costly compared to a LinkedList.
32. Which of these classes should be preferred to be used as a key in a HashMap?
Answer:
Explanation:
All of the classes listed (String, Integer, and Double) override the hashCode() and equals() methods, making them suitable to be used as keys in a HashMap. In fact, any class that correctly overrides these methods can be used as a key. However, it's essential that the key remains immutable while it's in the map to ensure the map's integrity. Among the options given, String, Integer, and Double are all immutable, so they are good choices for keys in a HashMap.
❮ Previous Quiz Next Quiz ❯
Comments
Post a Comment
Leave Comment