This post contains a few useful Java collections framework multiple-choice questions to self-test your knowledge of Java collections framework classes and interfaces.
1. Which interface provides key-value pair?
a. Listb. Set
c. Map
d. Collection
Answer
c. Map
Explanation
A map cannot contain duplicate keys. And, each key is associated with a single value.
2. What is the implementation of the List interface?
a. HashMapb. HashSet
c. LinkedList
d. LinkedHashSet
Answer
c. LinkedList
3. What are the implementation classes of the List interface?
b. HashSet
c. LinkedList
d. Vector
Answer
a, c, d
4. What are the implementation classes of the Set interface?
b. LinkedHashSet
c. ArrayList
d. TreeSet
Answer
a,b,d
5. What are the implementation classes of the Map interface?
a. HashMapb. LinkedHashMap
c. TreeMap
d. All of above
Answer
d. All of above
6. What are concurrent-aware interfaces?
a. Listb. BlockingQueue
d. SortedMap
Answer
b, c
7. What are concurrent-aware implementation classes?
a. TreeMapb. CopyOnWriteArrayList
c. PriorityBlockingQueue
d. ConcurrentHashMap
Answer
b,c,d
8. Choose the correct option based on this program:
import java.util.*;
class UtilitiesTest {
public static void main(String[] args) {
List < int > intList = new ArrayList < > ();
intList.add(10);
intList.add(20);
System.out.println("The list is: " + intList);
A. It prints the following: The list is: [10, 20]
B. It prints the following: The list is: [20, 10]
C. It results in a compiler error
D. It results in a runtime exception
Answer
C. It results in a compiler error
Explanation
You cannot specify primitive types along with generics, so List
9. What is the output of the following program?
import java.util.*;
class UtilitiesTest {
public static void main(String[] args) {
List < Integer > intList = new LinkedList < > ();
List < Double > dblList = new LinkedList < > ();
System.out.println("First type: " + intList.getClass());
System.out.println("Second type:" + dblList.getClass());
}
}
A. It prints the following:
First type: class java.util.LinkedList
Second type:class java.util.LinkedList
B. It prints the following:
First type: class java.util.LinkedList
Answer
A. It prints the following:
First type: class java.util.LinkedList
Second type:class java.util.LinkedList
Explanation
Due to type erasure, after compilation, both types are treated as the same LinkedList type
10. What is the output of the following program?
public class Question_7_1 {
public static void main(String[] args) {
ArrayDeque<Integer> deque =
new ArrayDeque<Integer>();
deque.push(1);
deque.push(2);
deque.push(3);
deque.poll();
System.out.println(deque);
}
}
A. [1, 2, 3]Answer
The correct answer is C.
Explanation
The push() inserts the element at the front of the deque. After pushing 1, 2, 3 the queue looks like [3, 2, 1].The poll() retrieves and removes the first element of this deque, 3 in this case.
11. Which of the following options can throw a NullPointerException?
A.
TreeSet<String> s = new TreeSet<>();
s.add(null);
B.
HashMap<String, String> m = new HashMap<>();
m.put(null, null);
C.
ArrayList<String> arr = new ArrayList<>();
arr.add(null);
D.
HashSet<String> s = new HashSet<String>();
s.add(null);
Answer
The correct answer is A.
Explanation
TreeSet doesn't allow null values because when you add an object if no Comparator is passed to the constructor of the TreeSet (like in this case), this class assumes that the object implements Comparable and tries to call the compareTo() method.12. Which of these maintains insertion order?
a.Listb.Set
c.All
d.None
Answer
a. List
13. Which Interface does not allow duplicates?
Answer
b. Set
14. Which data structure ArrayList internally uses?
Answer
b. Array
15. HashSet internally uses?
a.Set
b.HashMap
c.List
d.Collection
Answer
b.HashMap
16. Which is the root interface of the Java Collection framework hierarchy?
a.Collection
b.Root
c.Collections
d.List/Set
Answer
a.Collection
17. Which is best suited to a multi-threaded environment?
a.WeakHashMapb.Hashtable
c.HashMap
d.ConcurrentHashMap
Answer
d.ConcurrentHashMap
18. Which of these is synchronized?
a.TreeMap
b.HashMap
c.Hashtable
d.All
Answer
c.Hashtable
19. Which of these classes should be preferred to be used as a key in a HashMap?
a.String
b.Integer
c.Double
d.Any of these
Answer
a.String
20. Iterator returned by CopyOnWriteArraySet is
a.Fail-fast
b.Fail-safe
c.none
Answer
b.Fail-safe
Related Posts
- Java String Quiz
- Java Arrays Quiz
- Java Loops Quiz
- Java OOPS Quiz
- Java OOPS Quiz - Part 1
- Java OOPS Quiz - Part 2
- Java Exception Handling Quiz
- Java Collections Quiz
- Java Generics Quiz
- Java Multithreading Quiz
- JDBC Quiz
- Java Lambda Expressions Quiz
- Java Functional Interfaces Quiz
- Java Streams API Quiz
- Java Date Time Quiz
- Java 8 Quiz
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course