Search Tutorials


Java Collections MCQ - Multiple Choice Questions And Answers | JavaInUse

Java Collections MCQ - Multiple Choice Questions And Answers

Q. What is the difference between a List and a Set in Java Collections?

A. A List allows duplicate elements, while a Set does not
B. A List does not allow duplicate elements, while a Set does
C. A List is ordered, while a Set is not
D. A List is not ordered, while a Set is

Q. Which Java Collection class provides a key-value mapping?

A. List
B. Set
C. Map
D. Queue

Q. Which Java Collection class allows null elements?

A. List
B. Set
C. Map
D. Queue

Q. Which Java Collection class provides a LIFO (Last-In-First-Out) ordering?

A. List
B. Set
C. Map
D. Queue

Q. Which Java Collection class maintains the insertion order?

A. List
B. Set
C. Map
D. Queue

Q. Which Java Collection class provides a sorted order?

A. List
B. Set
C. Map
D. Queue

Q. Which Java Collection class provides a First-In-First-Out (FIFO) ordering?

A. List
B. Set
C. Map
D. Queue

Q. What is the purpose of the Comparator interface in Java Collections?

A. To provide a default ordering of elements
B. To define custom sorting logic
C. To filter elements based on a condition
D. To transform values

Q. What is the purpose of the Predicate functional interface in Java 8?

A. To produce results
B. To consume values
C. To filter elements based on a condition
D. To transform values

Q. Which Java Collection class provides a resizable array?

A. List
B. Set
C. Map
D. Queue

Q. What is the correct way to create an ArrayList in Java?

A.
ArrayList<String> list = new ArrayList<>();
B.
ArrayList list = new ArrayList();
C.
ArrayList<String> list = new ArrayList<String>();
D.
ArrayList<String> list = new ArrayList<String>;

Q. What is the difference between ArrayList and LinkedList in Java?

A.
ArrayList is an ordered collection while LinkedList is an unordered collection.
B.
ArrayList is faster for insertion and removal operations while LinkedList is faster for random access operations.
C.
ArrayList uses a doubly linked list data structure while LinkedList uses an array data structure.
D.
There is no difference between ArrayList and LinkedList in Java.





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

Set<Integer> numbers = new HashSet<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);

System.out.println(numbers.size());
A.
1
B.
2
C.
3
D.
5

Q. Which of the following is true about HashMap in Java?

A.
HashMap allows null keys and null values.
B.
HashMap is a synchronized collection.
C.
HashMap maintains insertion order of its elements.
D.
HashMap is an ordered collection.

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

Map<String, Integer> ages = new HashMap<>();
ages.put("John", 25);
ages.put("Emma", 30);
ages.put("Michael", 35);

ages.remove("John");

System.out.println(ages.containsKey("John"));
A.
true
B.
false
C.
null
D.
Compilation error

Q. Which of the following is an ordered collection in Java?

A.
ArrayList
B.
HashSet
C.
HashMap
D.
LinkedList

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

List>String< colors = new ArrayList><();
colors.add("Red");
colors.add("Green");
colors.add("Blue");

for (String color : colors) {
    if (color.equals("Green")) {
        colors.remove(color);
    }
}

System.out.println(colors.size());
A.
0
B.
1
C.
2
D.
Compilation error

Q. Which of the following collections in Java does not allow duplicate values?

A.
ArrayList
B.
HashSet
C.
LinkedList
D.
HashMap

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

Map<String, Integer> numbers = new HashMap<>();
numbers.put("One", 1);
numbers.put("Two", 2);
numbers.put("Three", 3);

System.out.println(numbers.get("One"));
A.
null
B.
0
C.
1
D.
Compilation error