Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

41 Java Collection MCQ

The document provides a list of various projects related to Spring Boot, React JS, and MySQL, along with their corresponding YouTube links. It also includes multiple-choice questions (MCQs) on Java Collections, covering topics such as List, ArrayList, Hashtable, and Queue, with explanations for each answer. Additionally, it promotes a premium subscription for CDAC notes and encourages joining a community for updates.

Uploaded by

Shobhit Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

41 Java Collection MCQ

The document provides a list of various projects related to Spring Boot, React JS, and MySQL, along with their corresponding YouTube links. It also includes multiple-choice questions (MCQs) on Java Collections, covering topics such as List, ArrayList, Hashtable, and Queue, with explanations for each answer. Additionally, it promotes a premium subscription for CDAC notes and encourages joining a community for updates.

Uploaded by

Shobhit Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Explore More

Subcription : Premium CDAC NOTES & MATERIAL @99

Contact to Join Click to Join


Premium Group Telegram Group

For More E-Notes


Join Our Community to stay Updated

TAP ON THE ICONS TO JOIN!


Project List

pg. 1 contact us on 8007592194 / 9284926333 www.codewitharrays.in


Project List

pg. 2 contact us on 8007592194 / 9284926333 www.codewitharrays.in


Project List

pg. 3 contact us on 8007592194 / 9284926333 www.codewitharrays.in


Project List

Spring Boot + React JS + MySQL Project List


Sr.No Project Name YouTube Link
1 Online E-Learning Hub Platform Project https://youtu.be/KMjyBaWmgzg?si=YckHuNzs7eC84-IW
2 PG Mate / Room sharing/Flat sharing https://youtu.be/4P9cIHg3wvk?si=4uEsi0962CG6Xodp
3 Tour and Travel System Project Version 1.0 https://youtu.be/-UHOBywHaP8?si=KHHfE_A0uv725f12
4 Marriage Hall Booking https://youtu.be/VXz0kZQi5to?si=llOS-QG3TpAFP5k7
5 Ecommerce Shopping project https://youtu.be/vJ_C6LkhrZ0?si=YhcBylSErvdn7paq
6 Bike Rental System Project https://youtu.be/FIzsAmIBCbk?si=7ujQTJqEgkQ8ju2H
7 Multi-Restaurant management system https://youtu.be/pvV-pM2Jf3s?si=PgvnT-yFc8ktrDxB
8 Hospital management system Project https://youtu.be/IynIouBZvY4?si=CXzQs3BsRkjKhZCw
9 Municipal Corporation system Project https://youtu.be/cVMx9NVyI4I?si=qX0oQt-GT-LR_5jF
10 Tour and Travel System Project version 2.0 https://youtu.be/_4u0mB9mHXE?si=gDiAhKBowi2gNUKZ

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?

(a) Just like an array, List indexes start from 0.


(b) List interface has got many default methods in Java 8.
(c) List allows you to have 'null' elements.
(d) List doesn't support Generics.

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

(a) ArrayList<String> list = new ArrayList[];


(b) ArrayList list = new List<String>();
ys

(c) ArrayList list = new ArrayList<String>();


ra

(d) ArrayList<String> list = new ArrayList<>();


ar
ith

Answer: (d) ArrayList<String> list = new ArrayList< >();


w
de

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.

Q#4. What is the output of the following code?

ArrayList<String> list = new ArrayList<>();


list.add("Java");
list.add("Python");

94
System.out.println(list.remove(0));

21
59
(a) Java
07
80
(b) Python
(c) true
.in

(d) Compilation error


ys
ra

Answer: a) Java
ar

Explanation: The remove() method is used to remove an element at the specified index in an
ith

ArrayList. In this case, it removes “Java” at index 0 and returns it.


w

Q#5. Which of the following is incorrect statement about the Hastable class?
de
co

(a) Hashtable class implements Map interface.


(b) Hashtable class extends the Dictionary class.
(c) Hashtable class implements Cloneable interface.
(d) Hashtable class extends Properties class.

Answer: d) Hashtable class extends Properties class.

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

Answer: (a), (b), (c), (d)

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

Answer: c) for (Object obj : list)


ra
ar

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

(a) It is typically pronounced as "deck".


(b) It stands for doubly-ended queue.
(c) It is mostly used to implement some algorithms and data structures.
(d) It can either be used as a queue(FIFO) or as a stack(LIFO).

Answer: b) It stands for doubly-ended queue.

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.

Q#11. What is the output of the following code?

LinkedList<String> list = new LinkedList<>();


list.add("Java");
list.add("Python");
System.out.println(list.poll());

(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().

Q#14. Which method is used to add an element to the end of an ArrayList?

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.

Q#16. Which method is used to check if a specific element is present in a Set?


(a) containsElement()
(b) containsKey()
(c) containsValue()
(d) contains()

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

Q#18. Which collection class is synchronized and thread-safe?


de
co

(a) ArrayList
(b) HashSet
(c) ConcurrentHashMap
(d) LinkedList

Answer: c) ConcurrentHashMap

Explanation: The ConcurrentHashMap class is synchronized and thread-safe, allowing multiple


threads to access it simultaneously without data discrepancies.
Q#19. Which collection class in Java provides a fixed-size collection that does not allow
adding or removing elements?

(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?

LinkedHashSet<Integer> set = new LinkedHashSet<>();


set.add(10);
set.add(20);
set.add(30);
System.out.println(set);

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

Q#23. What is the output of the following code?


ar
ith

ArrayList<String> list = new ArrayList<>();


w

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.

Q#24. Which method is used to obtain a synchronized (thread-safe) version of a List


collection?

(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

TreeSet<Integer> set = new TreeSet<>();


ra

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

(a) [Java, Python]


(b) [Python, Scala]
de

(c) [Python]
co

(d) [Scala]

Answer: b) [Python, 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].

Q#28. What is the output of the following code?


LinkedHashSet<String> set = new LinkedHashSet<>();
set.add("Java");
set.add("Python");
System.out.println(set.remove("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.

Q#30. What is the output of the following code?

HashSet<Integer> set1 = new HashSet<>();


set1.add(1);
set1.add(2);
set1.add(3);

HashSet<Integer> set2 = new HashSet<>();


set2.add(2);
set2.add(3);
set1.add(4);

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

Q#31. What is the output of the following code?


ar
ith
w

HashMap<String, Integer> map = new HashMap<>();


de

map.put("Java", 10);
map.put("Python", 20);
co

System.out.println(map.keySet());

(a) ["Java", "Python"]


(b) [10, 20]
(c) "Java", "Python"
(d) [Java, Python]
Answer: d) [Java, Python]

Explanation: The keySet() method is used to retrieve a set of all the keys in a HashMap. In this case, it
returns [Java, Python].

Q#32. Which method is used to check if a LinkedList is empty?

(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

LinkedList<String> list = new LinkedList<>();


ys

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?

(a) The newly added element is immediately visible to all iterators.


(b) Iterators throw a ConcurrentModificationException.
(c) Iterators continue without considering the newly added element.
(d) The iteration blocks until the addition is complete.

Answer: c) Iterators continue without considering the newly added element.

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

(d) TreeSet allows null values, while HashSet does not.


ys
ra
ar

Answer: c) HashSet is unsorted, while TreeSet is sorted.


ith

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?

(a) ArrayList is faster than LinkedList.


(b) LinkedList is faster than ArrayList.
(c) Both ArrayList and LinkedList have the same performance.
(d) It depends on the size 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

(logarithmic time complexity) on large collections.

Q#39. What is the output of the following code snippet?

List<Integer> list = new ArrayList<>();


list.add(4);
list.add(2);
list.add(3);
list.add(2);
System.out.println(list.lastIndexOf(2));

(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

(a) CopyOnWriteArrayList class implements List and RandomAccess interfaces.


ys

(b) CopyOnWriteArrayList is a concurrent replacement for a synchronized List.


(c) It allows duplicate elements and heterogeneous Objects.
ra

(d) It throws ConcurrentModificationException.


ar
ith
w

Answer: (a), (b), (c)


de

Explanation: CopyOnWriteArrayList is one of the solution for ConcurrentModificationException.


co

Hence it can’t throw ConcurrentModificationException. All other statements are true


for CopyOnWriteArrayList.

Q#41. What is a key feature of a Java Map object?

a) It allows any value as a key


b) It maintains insertion order of elements
c) It automatically removes duplicate keys
d) It stores key-value pairs
Answer: d) It stores key-value pairs

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/

https://t.me/codewitharrays Group Link: https://t.me/ccee2025notes

+91 8007592194 +91 9284926333

codewitharrays@gmail.com

https://codewitharrays.in/project

You might also like