41 Java Collection MCQ
41 Java Collection MCQ
Sr.No
Project Name YouTube Link
11 Tour and Travel System Project version 3.0 https://youtu.be/Dm7nOdpasWg?si=P_Lh2gcOFhlyudug
12 Gym Management system Project https://youtu.be/J8_7Zrkg7ag?si=LcxV51ynfUB7OptX
13 Online Driving License system Project https://youtu.be/3yRzsMs8TLE?si=JRI_z4FDx4Gmt7fn
14 Online Flight Booking system Project https://youtu.be/m755rOwdk8U?si=HURvAY2VnizIyJlh
15 Employee management system project https://youtu.be/lD1iE3W_GRw?si=Y_jv1xV_BljhrD0H
16 Online student school or college portal https://youtu.be/4A25aEKfei0?si=RoVgZtxMk9TPdQvD
17 Online movie booking system project https://youtu.be/Lfjv_U74SC4?si=fiDvrhhrjb4KSlSm
18 Online Pizza Delivery system project https://youtu.be/Tp3izreZ458?si=8eWAOzA8SVdNwlyM
19 Online Crime Reporting system Project https://youtu.be/0UlzReSk9tQ?si=6vN0e70TVY1GOwPO
20 Online Children Adoption Project https://youtu.be/3T5HC2HKyT4?si=bntP78niYH802I7N
pg. 4 contact us on 8007592194 / 9284926333 www.codewitharrays.in
MCQ on Collections In Java
Q#1. Which of the following statement(s) is/are correct about the List collection?
94
Answer: (a), (b), (c)
21
Explanation: The List interface indexes start from 0, allows null elements & also supports Generics. It
allows duplicate elements and maintains an ordered collection. Furthermore, List interface has got
59
many default methods in Java 8.
07
Q#2. What is the correct way to create an ArrayList of Strings?
80
.in
Explanation: While creating an ArrayList of a specific type, the variable declaration should include the
type.
co
Q#3. Which collection allows you to retrieve elements in the order they were inserted?
(a) ArrayList
(b) LinkedList
(c) LinkedHashMap
(d) LinkedHashSet
Answer: (a), (b), (c), (d)
Explanation: The LinkedList & ArrayList classes implement the List interface and allows us to retrieve
elements based on the insertion order. LinkedHashMap is a map implementation that maintains the
insertion order of key-value pairs. Similarly, the LinkedHashSet is a set implementation that
maintains the insertion order of elements.
94
System.out.println(list.remove(0));
21
59
(a) Java
07
80
(b) Python
(c) true
.in
Answer: a) Java
ar
Explanation: The remove() method is used to remove an element at the specified index in an
ith
Q#5. Which of the following is incorrect statement about the Hastable class?
de
co
Explanation: Hashtable class doesn’t extend Properties class. Alternatively, the Properties class
extends Hashtable class.
Q#6. Which of the following collection class/(s) is/are synchronized and thread-safe?
(a) Stack
(b) Hashtable
(c) Properties
(d) Vector
Explanation: All of the above classes are synchronized and thread safe in java.
94
Q#7. Which one is the correct way to iterate over elements in an ArrayList?
21
59
(a) for (int i = 0; i < list.size(); i++)
(b) for (int i : list)
07
80
(c) for (Object obj : list)
(d) for (Iterator it = list.iterator(); it.hasNext();)
.in
ys
Explanation: The correct way to iterate over elements in an ArrayList is by using a for-each loop and
declaring the loop variable as the type of the elements in the list.
ith
Q#8. Which of the following statement is incorrect about Deque interface in Java?
w
de
co
Explanation: The Deque interface extends the Collection interface and adds support for inserting
elements at both the ends. It stands for “double-ended queue”, not the doubly-ended queue. All
other statements are correct about Deque. You may also read When to use Deque in some more
detail.
Q#9. Suppose you are working in a Java project. You have a requirement where you need a
dynamic or resizable array-like data structure that can permit you to add, remove, and access
elements at a specific index efficiently. It should also automatically adjust its size when the
elements are added or removed. Which of the following class will you use to satisfy your
requirement?
(a) Vector
(b) LinkedList
(c) ArrayList
94
(d) HashMap
21
59
Answer: c) ArrayList
07
Explanation: The ArrayList class provides a resizable array implementation of the List interface. It
80
dynamically grows and shrinks as elements are added or removed.
.in
Q#10. Which collection class provides a way to store elements in key-value pairs with no
duplicate keys?
ys
ra
(a) ArrayList
ar
(b) Hashtable
ith
(c) TreeMap
(d) LinkedHashMap
w
de
co
Answer: c) TreeMap
Explanation: The TreeMap class provides a way to store elements in key-value pairs with no duplicate
keys. It orders the keys in ascending order by default.
(a) Java
(b) Python
(c) true
(d) Compilation error
94
21
Answer: a) Java
Explanation: The poll() method of LinkedList retrieves and removes the first element in a LinkedList.
59
In this case, it retrieves and removes ‘Java’.
07
Q#12. Which one of the following collection classes can be used to store unique & sorted
objects?
80
.in
(a) HashSet
ys
(b) TreeSet
(c) LinkedHashMap
ra
(d) ArrayList
ar
ith
w
Answer: b) TreeSet
de
Explanation: The TreeSet class provides a way to store unique elements in a sorted order. It can order
co
the elements using ascending order. TreeSet does not preserve the insertion order of elements but
elements are sorted by keys.
Q#13. Apart from providing methods for adding, removing, and examining elements in a
specific order, which of the following interfaces provides methods like peek() and poll()?
(a) Set
(b) List
(c) Queue
(d) Map
Answer: c) Queue
Explanation: The Queue interface provides methods for adding, removing, and examining elements
in a specific order, following the FIFO (First-In-First-Out) principle. It also includes additional queue-
specific operations like peek() and poll().
94
21
(a) addLast()
(b) addEnd()
59
(c) add()
07
(d) append() 80
.in
Answer: c) add()
ys
Explanation: The add() method itself is used to add an element to the end of an ArrayList.
ra
Q#15. Which collection class allows duplicate elements and maintains their insertion order?
ar
ith
(a) HashSet
w
(b) TreeSet
de
(c) LinkedHashSet
(d) HashMap
co
Answer: c) LinkedHashSet
Explanation: The LinkedHashSet class allows duplicate elements and maintains their insertion order.
It combines the features of a HashSet and a LinkedList.
Answer: d) contains()
Explanation: The contains() method is used to check if a specific element is present in a Set. If the
element is found, It returns true, otherwise false.
94
Q#17. Which collection class stores objects in the form of key-value pairs and maintains the
21
order in which they were inserted?
59
07
(a) Hashtable
(b) LinkedHashMap
80
(c) TreeMap
(d) HashSet
.in
ys
ra
Answer: b) LinkedHashMap
ar
Explanation: The LinkedHashMap class provides a way to store key-value pairs. It maintains the
insertion order of the elements.
ith
w
(a) ArrayList
(b) HashSet
(c) ConcurrentHashMap
(d) LinkedList
Answer: c) ConcurrentHashMap
(a) ArrayList
(b) LinkedList
(c) HashSet
(d) Arrays
Answer: d) Arrays Explanation: The Arrays class in Java provides utility methods for working with fixed-size
arrays, which do not allow adding or removing elements once initialized.
94
Q#20. Which method is used to retrieve the first element from a Queue without removing it?
21
59
(a) peek()
(b) getFirst()
07
80
(c) element()
(d) front()
.in
ys
Answer: a) peek()
ra
ar
Explanation: The peek() method is used to retrieve the first element from a Queue without removing
it. If the Queue is empty, it returns null.
ith
Q#21. Which interface should be implemented to create a custom Comparator for sorting
w
objects in a TreeSet?
de
co
(a) Comparable
(b) Sortable
(c) Comparator
(d) ComparatorSet
Answer: c) Comparator
Explanation: To create a custom Comparator for sorting objects in a TreeSet, the Comparator
interface should be implemented.
Q#22. What is the output of the following code?
94
(a) 10 20 30
21
(b) [30, 20, 10]
(c) [10, 20, 30]
59
(d) Compilation error
07
80
Answer: c) [10, 20, 30]
.in
Explanation: The LinkedHashSet maintains the insertion order of elements. It will output the
elements in the order they were added.
ys
ra
list.add("Java");
list.add("Python");
de
System.out.println(list.contains("Python"));
co
(a) true
(b) false
(c) Python
(d) Compilation error
Answer: a) true
Explanation: The contains() method is used to check if a specific element exists in the ArrayList. In
this case, it returns true as “Python” is present.
(a) synchronized()
(b) synchronizedList()
(c) synchronizedCollection()
(d) synchronize()
94
21
59
Answer: b) synchronizedList()
07
Explanation: The synchronizedList() method is used to obtain a synchronized (thread-safe) version of
a List collection, such as ArrayList.
80
Q#25. What is the output of the following code?
.in
ys
set.add(24);
set.add(5);
ar
set.add(50);
ith
System.out.println(set.first());
w
de
co
(a) 10
(b) 5
(c) 20
(d) Compilation error
Answer: b) 5
Explanation: The first() method of the TreeSet is used to retrieve the first (lowest) element. In this
case, it retrieves the smallest element, which is 5.
Q#26. Which method is used to retrieve the last element without removing it from a
LinkedList?
(a) getLast()
(b) lastElement()
(c) getLastElement()
(d) peekLast()
Answer: a) getLast()
94
Explanation: The getLast() method is used to retrieve the last element from a LinkedList. It returns
21
the last element without removing it from the list.
59
Q#27. What is the output of the following code snippet?
07
80
List<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
.in
list.add("Scala");
ys
System.out.println(list.subList(1, 3));
ra
ar
ith
w
(c) [Python]
co
(d) [Scala]
Explanation: The subList(1, 3) method returns a fraction of the list from index 1 (inclusive) to index 3
(exclusive), so the output is [Python, Scala].
(a) true
(b) false
94
(c) Java
(d) Compilation error
21
59
Answer: b) false
07
Explanation: The remove() method is used to remove a specific element from a LinkedHashSet. In
80
this case, “Scala” is not present, so it returns false.
.in
Q#29. Which method is used to retrieve the key associated with the maximum value in a
TreeMap?
ys
ra
ar
(a) getMaxKey()
(b) firstKey()
ith
(c) lastKey()
w
(d) getMaxValue()
de
co
Answer: c) lastKey()
Explanation: The lastKey() method is used to retrieve the key associated with the largest value in a
TreeMap. Since the TreeMap is ordered by keys, the largest key is the last key.
set1.retainAll(set2);
System.out.println(set1);
94
21
(a) [1, 4]
(b) [2, 3]
59
(c) [1, 2, 3]
(d) [1, 2, 3, 4]
07
80
.in
Answer: b) [2,3]
ys
Explanation: The retainAll() method is used to retain only the elements that exist in both sets. In this
case, the intersection of set1 and set2 is [2,3].
ra
map.put("Java", 10);
map.put("Python", 20);
co
System.out.println(map.keySet());
Explanation: The keySet() method is used to retrieve a set of all the keys in a HashMap. In this case, it
returns [Java, Python].
(a) isEmpty()
(b) checkEmpty()
(c) hasElements()
(d) hasData()
94
21
Answer: a) isEmpty()
59
Explanation: The isEmpty() method is used to check if a LinkedList is empty. It returns true if the
07
LinkedList contains no elements.
80
Q#33. What is the output of the following code?
.in
list.add("Java");
ra
list.add("Python");
list.add("Scala");
ar
System.out.println(list.removeLast());
ith
w
de
co
(a) Java
(b) Python
(c) Scala
(d) Compilation error
Answer: c) Scala
Explanation: The removeLast() method is used to retrieve and remove the last element in a
LinkedList. In this case, it retrieves and removes “Scala”.
Q#34. What happens when an element is added to a CopyOnWriteArrayList while multiple
threads are iterating over it?
94
Explanation: CopyOnWriteArrayList iterators operate on a copy of the underlying array, so they do
21
not see modifications made after the iterator was created.
59
Q#35. What is the main difference between HashSet and TreeSet in Java?
07
80
(a) HashSet allows duplicate elements, while TreeSet does not.
(b) HashSet maintains insertion order, while TreeSet does not.
(c) HashSet is unsorted, while TreeSet is sorted.
.in
Explanation: HashSet does not maintain any specific order, while TreeSet sorts elements in a specific
w
order.
de
Q#36. Which collection class in Java does not allow null values?
co
(a) HashSet
(b) ArrayList
(c) HashMap
(d) TreeSet
Answer: d) TreeSet
Explanation: TreeSet does not allow null values because it uses the natural ordering of elements or a
custom comparator, which cannot compare null.
Q#37. What is the difference between ArrayList and LinkedList in terms of inserting an
element in the middle of the collection?
94
21
Answer: b) LinkedList is faster than ArrayList.
59
Explanation: LinkedList performs better than ArrayList when inserting elements in the middle
because it does not require shifting elements.
07
Q#38. Which collection class in Java is best suited for frequent search operations on a large
80
collection?
.in
ys
(a) HashSet
(b) ArrayList
ra
(c) LinkedList
ar
(d) TreeMap
ith
w
Answer: d) TreeMap
de
Explanation: TreeMap uses a balanced tree structure, providing efficient search operations
co
(a) 1
(b) 2
(c) 3
(d) 4
94
21
Answer: c) 3
59
Explanation: The lastIndexOf(2) method returns the index of the last occurrence of the specified
element in the list. In this case, the last occurrence of 2 is at index 3, so the output is 3.
07
Q#40. Which statement(s) is/are true about CopyOnWriteArrayList in Java?
80
.in
Explanation: The primary function of a Java Map is to store key-value pairs, allowing for efficient
retrieval, insertion, and updating of elements based on their keys. Although Java Maps can use
various types of objects as keys, not all values are valid keys. For example, keys must correctly
implement hashCode() and equals() methods. Insertion order behavior is specific to certain
implementations like LinkedHashMap, but it is not a general feature of all Java Map
implementations. Java Maps do not allow duplicate keys. If a key that already exists in the map is
added, the new value will overwrite the old value associated with that key.
94
21
59
07
80
.in
ys
ra
ar
ith
w
de
co
https://www.youtube.com/@codewitharrays
https://www.instagram.com/codewitharrays/
codewitharrays@gmail.com
https://codewitharrays.in/project