This collection of Java Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “Java Collections”.
1. Which of these packages contains all the collection classes?
A java.awt
B java.net
C java.util
D java.lang
C
All the classes(ArrayList, SortedList, Map, etc) of the Framework Collections are located in java.util package.
2. Which of these classes is not part of the Collection framework in Java?
A Queue
B Stack
C Array
D Map
D
Map class is not part of the collection framework.
3. Which of these interfaces is not part of the Collection framework in Java?
A SortedMap
B SortedList
C Set
D List
B
SortedList interface is not part of the collection framework.
[st_adsense]
4. Which of the following methods removes all items from a collection?
A refresh()
B delete()
C reset()
D clear()
D
clear() method removes all items from a collection. Example:
// create an empty ArrayList
ArrayList<String> list = new ArrayList<String>();
// Adding elements to the list
list.add("Alex");
list.add("Jean");
list.add("Bob");
// display the list before clearing it
System.out.println("Before clearing: " + list);
// Clearing the list
list.clear();
// display the list after clearing it
System.out.println("After clearing: " + list);
Output:
Before clearing: [Alex, Jean, Bob]
After clearing: []
5. What is Collection in Java?
A A group of objects
B A group of interfaces
C A group of classes
D None of the above
A
Collection is an object that represents a group of objects.
6. Which of these interfaces is not part of the collection framework in Java?
A Collection
B Set
C Group
D List
C
Group is not part of the collection framework.
7. Which interface does not allow duplicates elements?
A Set
B List
C Map
D All the answers are true
A
Set interface does not allow duplicates elements. Example:
Set uniqueNames = new HashSet();
uniqueNames.add("Alex");
uniqueNames.add("Alex"); /* No error and code works fine
but doesn't add duplicate value */
uniqueNames.add("Bob");
uniqueNames.add("Jean");
for (String names : uniqueNames){
System.out.println(names);
}
Output:
Alex
Bob
Jean
[st_adsense]
8. Which of these collection classes has the ability to scale dynamically?
A Array
B Arrays
C ArrayList
D All the answers are true
C
ArrayList is a resizable array that implements the List interface.
9. HashMap allows _____________
A null values
B null key
C All the answers are true
D None of the above
C
HashMap allows one null key and null values (only one null key is allowed because two keys are not allowed). On the other hand, Hashtable does not allow null keys or null values.
10. The effectiveness of a HashMap can be guaranteed by __________
A Overriding the equals method
B Overriding the hashCode method
C All the answers are true
D None of the above
C
HashMap relies on the equals() and hashCode() method to compare keys and values.
MCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More
[st_adsense]