InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
How can you make an ArrayList read-only in Java? |
|
Answer» With the help of Collections.unmodifiableList() method, we can easily make an ArrayList read-only. This function takes a CHANGEABLE ArrayList as an input and returns the ArrayList's read-only, unmodified view. Example: import java.util.*; public class InterviewBit { public static void MAIN(String[] args) throws Exception { try { // creating object of ArrayList<String> LIST<String> sample_list = new ArrayList<String>(); sample_list.add(“practice”); sample_list.add(“SOLVE”); sample_list.add(“interview”); // DISPLAYING the initial list System.out.println("The initial list is : " + sample_list); // using unmodifiableList() method List<String> read_only_list = Collections .unmodifiableList(sample_list); // displaying the read-only list System.out.println("The ReadOnly ArrayList is : " + read_only_list); // Trying to add an element to the read-only list System.out.println("Trying to modify the ReadOnly ArrayList."); read_only_list.add(“job”); } catch (UnsupportedOperationException e) { System.out.println("The exception thrown is : " + e); } }}Output: The initial list is : [practice, solve, interview]The ReadOnly ArrayList is : [practice, solve, interview]Trying to modify th eReadOnly ArrayList.Exception thrown : java.lang.UnsupportedOperationExceptionWe can see that as we try to add an element to a read-only ArrayList we get an exception thrown. |
|
| 2. |
Differentiate between Array and ArrayList in Java. |
|
Answer» Following are the differences between Arrays and ArrayLists in Java :
|
|
| 3. |
What is the default size of the load factor in hashing based collection? |
|
Answer» The DEFAULT LOAD factor size is 0.75. The default capacity is CALCULATED by MULTIPLYING the initial capacity by the load factor. |
|
| 4. |
Differentiate between HashSet and HashMap. |
||||||||||||||
|
Answer» HashSet is a Set Interface implementation that does not allow duplicate values. The essential point is that objects STORED in HashSet must override equals() and HASHCODE() methods to ensure that no duplicate values are stored in our set. HASHMAP is a Map Interface implementation that maps a key to a value. In a map, duplicate keys are not permitted.
|
|||||||||||||||
| 5. |
Differentiate between Set and Map in Java. |
||||||||
|
Answer» The Set interface is provided by the Java.util package. The set interface is established by extending the COLLECTION interface. We can't add the same element to it since it won't let US. Because it contains elements in a sorted order, it does not keep the insertion order. The Set interface in Java is used to build the MATHEMATICAL Set. Map is SIMILAR to Set in that it is used to store a collection of objects as a single entity. A KEY-value pair is used to store each object. Because each value is associated with a unique key, we can quickly obtain the value using just the key.
|
|||||||||
| 6. |
What are some of the best practices while using Java Collections? |
|
Answer» Following are some of the best practices while using Java Collections :
|
|
| 7. |
What is a priority queue in Java? |
|
Answer» When the objects are MEANT to be processed in order of priority, a PriorityQueue is used. A Queue is known to follow the First-In-First-Out method, however, there are occasions when the components of the queue must be handled in order of priority, which is where the PriorityQueue comes into PLAY. The priority HEAP is the foundation of the PriorityQueue. The members of the priority queue are ORDERED according to NATURAL ordering or by a Comparator provided at queue construction time. Serializable, Iterable<E>, Collection<E>, Queue<E> interfaces are implemented by the PriorityQueue class in Java. |
|
| 8. |
Can you add a null element into a TreeSet or HashSet? |
|
Answer» We can add NULL ELEMENTS in a HASHSET but we cannot add null elements in a TreeSet. The reason is that TreeSet uses the compareTo() method for comparing and it throws a NullPointerException when it ENCOUNTERS a null ELEMENT. |
|
| 9. |
Differentiate between HashSet and TreeSet. When would you prefer TreeSet to HashSet? |
|
Answer» Following are the differences between HashSet and TreeSet:-
Following are the cases when TreeSet is preferred to HashSet :
|
|
| 10. |
Differentiate between Iterator and ListIterator in Java. |
||||||||||||
|
Answer» In Java's Collection framework, iterators are used to obtain elements one by one. It can be used on any TYPE of Collection object. We can execute both read and remove operations using Iterator. Iterator MUST be used whenever we want to iterate elements in all Collection framework implemented interfaces, such as Set, List, Queue, and Deque, as well as all Map interface implemented classes. The only cursor accessible for the entire collection framework is the iterator. ListIterator is only useful for classes that implement List collections, such as array lists and linked lists. It can iterate in both directions. When we WISH to enumerate List elements, we must use ListIterator. This cursor has additional methods and capabilities than the iterator.
|
|||||||||||||
| 11. |
Differentiate between List and Set in Java. |
||||||||||
|
Answer» The LIST interface is used to keep track of an ordered collection. It is the Collection's child interface. It is an ordered collection of objects that allows for the storage of DUPLICATE values. The insertion order is preserved in a list, which enables positional access and ELEMENT insertion. The set interface is PART of java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It's an interface for using the mathematical set. This interface inherits the Collection interface's METHODS and adds a feature that prevents duplicate elements from being inserted.
|
|||||||||||
| 12. |
Differentiate between ArrayList and Vector in java. |
|
Answer» Following are the differences between ArrayList and Vector in java :
|
|
| 13. |
Difference between ArrayList and LinkedList. |
||||||||||
Answer»
|
|||||||||||
| 14. |
Explain the various interfaces used in the Collection framework. |
|
Answer» The collection framework has several interfaces, each of which is used to store a different sort of data. The interfaces included in the framework are listed below. 1. Iterable Interface: This is the collection framework's primary interface. The iterable interface is extended by the collection interface. As a result, all interfaces and classes implement this interface by default. This interface's main purpose is to provide an iterator for the collections. As a result, this interface only has one abstract method, the iterator. 2. Collection Interface: The collection framework's classes implement this interface, which extends the iterable interface. This interface covers all of the basic methods that every collection has, such as adding data to the collection, removing data from the collection, clearing data, and so on. All of these methods are incorporated in this interface because they are used by all classes, regardless of their implementation style. Furthermore, including these methods in this interface guarantees that the method names are consistent across all collections. In summary, we may conclude that this interface LAYS the groundwork for the implementation of collection classes. 3. List Interface: The collection interface has a child interface called the list interface. This interface is devoted to list data, in which we can store all of the objects in an ordered collection. This also allows for the presence of redundant data. Various classes, such as ArrayList, Vector, Stack, and others, implement this list interface. We can create a list object with any of these classes because they all implement the list. 4. Queue Interface: A queue interface, as the name implies, follows the FIFO (First In First Out) order of a real-world queue line. This interface is for storing all elements in which the order of the elements is important. When we try to shop at a store, for example, the bills are issued on a first-come, first-served basis. As a result, the INDIVIDUAL whose request is first in line receives the bill first. PriorityQueue, DEQUE, ArrayDeque, and other classes are available. Because all of these subclasses implement the queue, we can use any of them to create a queue object. 5. Deque Interface: It differs slightly from the queue data structure. Deque, also known as a double-ended queue, is a data structure in which elements can be added and removed from both ends. The queue interface is extended by this interface. ArrayDeque is the class that implements this interface. Because this class implements the deque, we can use it to create a deque object. 6. Set Interface: A set is an unordered group of objects in which duplicate values cannot be kept. This collection is utilised when we want to avoid duplication of things and only keep the ones that are unique. Various classes, such as HASHSET, TREESET, LinkedHashSet, and others, implement this set interface. We can create a set object with any of these classes because they all implement the set. 7. Sorted Set Interface: This interface resembles the set interface in appearance. The only difference is that this interface provides additional methods for maintaining element ordering. The sorted set interface is an extension of the set interface that is used to manage sorted data. TreeSet is the class that implements this interface. We can create a SortedSet object using this class because it implements the SortedSet interface. |
|
| 15. |
What are the advantages of the Collection framework? |
|
Answer» FOLLOWING are the advantages of the Collection framework:- Consistent API: The API has a core set of interfaces like Collection, Set, List, or Map, and all the classes (ARRAYLIST, LinkedList, Vector, and so on) that implement these interfaces have some common set of methods. Cuts programming effort: Instead of worrying about the Collection's design, a programmer may concentrate on how BEST to USE it in his program. As a result, the fundamental principle of Object-oriented programming (i.e. abstraction) has been applied successfully. Improves program speed and quality by offering high-performance implementations of useful data STRUCTURES and algorithms, as the programmer does not have to worry about the optimum implementation of a certain data structure in this scenario. They can simply use the best implementation to improve the performance of their program significantly. |
|
| 16. |
Explain the hierarchy of the Collection framework in Java. |
|
Answer» The entire collection FRAMEWORK hierarchy is MADE up of FOUR fundamental INTERFACES: Collection, List, Set, Map, and two specific interfaces for SORTING called SortedSet and SortedMap. The java.util package contains all of the collection framework's interfaces and classes. The following diagram depicts the Java collection structure. Here, e denotes extends, i denotes implements
|
|
| 17. |
Differentiate between Collection and collections in the context of Java. |
||||||||
|
Answer» Collection : In the JAVA.util.package, there is an interface called a collection. It's USED to represent a collection of separate objects as a single entity. It's equivalent to the container in the C++ programming language. The collection framework's root interface is referred to as the collection. It has a number of classes and interfaces for representing a collection of individual objects as a single unit. The key sub-interfaces of the collection interface are List, Set, and Queue. Although the map interface is part of the Java collection framework, it does not inherit the interface's collection. The Collection interface's most significant functions are add(), remove(), clear(), size(), and CONTAINS(). Collections: The java.util.package has a utility class called Collections. It defines various utility methods for working with collections, such as sorting and searching. All of the methods are static. These techniques give developers much-needed convenience, allowing them to INTERACT with Collection Framework more successfully. It provides methods like sort() to sort the collection elements in the normal sorting order, and min() and max() to get the minimum and maximum value in the collection elements, respectively.
|
|||||||||
| 18. |
What is the difference between Array and Collection in java? |
||||||||||||||
|
Answer» Array and Collection are equivalent in terms of STORING object references and manipulating data, but they differ in a number of ways. The following are the primary distinctions between an array and a Collection:
|
|||||||||||||||