Java Collections Quiz-1

Java Collections Quiz-1 | In Java, collection framework classes are used to store elements in value or key-value format without size limitation. We have listed some basic and fundamental Java Collections Quiz, answer them.

Also see- Java Collection Framework Overview, Java Collection Interface, List Interface in Java

Q1) Which of the following are limitations of Array?

a) It is fixed in size.
b) It allows us to store only the same types of elements.
c) It doesn’t provide in-built methods to perform different operations on the array.
d) All of these.


Answer:- d) All of these.

Q2) In Java, how can we store different types of elements in an array?

a) By using Object type array
b) By using Integer type array
c) We can’t store different types of elements in the array.
d) None of these


Answer:- a) By using Object type array

The java.lang.Object is the superclass of all Java classes. The primitive values also can be represented as Objects using the Wrapper class. Hence by using java.lang.Object type array we can store different types of elements in the array.

Example:-
Object obj[] = new Object[10];
obj[0] = 10;
obj[1] = new Student();
obj[2] = new Test();
obj[3] = “Java”;


Q3) Array can hold?

a) Only Primitive values
b) Only Object type values
c) Both Primitive & Objects type values
d) None of these


Answer:- c) Both Primitive & Objects type values

In Java, Array can hold both primitives and objects.


Q4) Collection classes can hold?

a) Only Primitive values
b) Only Object type values
c) Both Primitive & Objects type values
d) None of these


Answer:- b) Only Objects type values

In Java, Array can hold both primitives and objects but Collection classes can hold only Object type values. All primitive values are converted to their respective Wrapper classes type.


Q5) Collection classes are defined in which packages?

a) java.io package
b) java.lang package
c) java.util package
d) java.sql package


Answer:- c) java.util package

All Collection classes are defined in java.util packages.


Q6) Collection(I), List(I), Set(I), Queue(I) and all its subclasses are meant for?

a) Represent a group of individual objects.
b) Represent a group of objects as key-value pairs.
c) Both (a) and (b)
d) None of these


Answer:- a) Represent a group of individual objects.

Q7) Map(I) and its subclasses are meant for?

a) Represent a group of individual objects.
b) Represent a group of objects as key-value pairs.
c) Both (a) and (b)
d) None of these


Answer:- b) Represent a group of objects as key-value pairs.

Collection(I), List(I), Set(I), Queue(I), and all its subclasses are meant for representing a group of individual objects. If we want to represent a group of objects as key-value pairs then we should go for Map(I).


Q8) Collection is a _ whereas Collections is __ ?

a) Interface, Interface
b) Interface, Class
c) Class, Interface
d) Class, Class


Answer:- b) Interface, Class

The Collection is an interface, if we want to represent a group of individual objects as a single entity then we should go for Collection(I) and its subclasses. Collections is a utility class present in java.util package to define several utility methods for Collection type objects like sorting, searching, and e.t.c.


Q9) Most of the Collection class implements?

a) Cloneable, Serializable
b) AutoClosable, Cloneable, Serializable
c) Cloneable
d) Serializable


Answer:- a) Cloneable, Serializable

Usually, we can use Collections to hold & transfer objects from one location to another location (container). To provide support for this requirement every collection class by default implements Cloneable, and Serializable interfaces.


Q10) Most of the collection classes allow storing?

a) Homogeneous Elements (All same type)
b) Heterogeneous Elements (Different Types)
c) Both (a) and (b)
d) None of these


Answer:- b) Heterogeneous Elements (Different Types)

Most of the collection classes allow the storage of Heterogeneous elements i.e. different types of elements.


Q11) Which of the following Collection class allow only Homogeneous elements?

a) ArrayList, LinkedList
b) HashMap, Hashtable
c) TreeMap, TreeSet
d) None of these


Answer:- c) TreeMap, TreeSet

In the entire Java collection framework, most of the classes allow to store heterogeneous elements but, TreeMap and TreeSet classes allow to store only homogeneous elements. They don’t allow to the storage of heterogeneous elements because every element is compared with other elements. To compare two elements they must be the same type.


Q12) Which of the following classes implements the RandomAccess interface?

a) ArrayList, Vector
b) ArrayList, LinkedList
c) HashMap, Hashtable
d) TreeMap, TreeSet


Answer:- a) ArrayList, Vector

ArrayList & Vector classes implements RandomAccess interface. Therefore in these 2 classes, we can access any random element with the same speed. We can retrieve nth elements directly without retrieving (n-1) elements.


If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *