1) What is framework in Java?
A framework is a popular and readymade architecture that contains a set of classes and interfaces.
2) What is the Collection framework in Java?
Collection Framework is a grouping of classes and interfaces that is used to store and manage the objects. It provides various classes like Vector, ArrayList, HashSet, Stack, etc. Java Collection framework can also be used for interfaces like Queue, Set, List, etc.
3) Explain Collections Class
java.util.Collections is a class consists of static methods that operate on collections. It contains polymorphic algorithms to operate on collections, “wrappers”. This class contains methods for algorithms, like binary sorting, search, shuffling, etc.
4) What is the hashCode()?
The hashCode() is a method that returns an integer hash code.
5) Distinguish between ArrayList and Vector in the Java collection framework.
6) What is ArrayList in Java?
ArrayList is a data structure that can be stretched to accommodate additional elements within itself and shrink back to a smaller size when elements are removed. It is a very important data structure useful in handling the dynamic behavior of elements.
7) Differentiate between Iterator and ListIterator
The difference between Iterator and ListIterator is:
8) What is the difference between Iterator and Enumeration?
The difference between Iterator and Enumeration
9) Define BlockingQueue
BlockingQueue is an interface used in Java that can extend the Queue. It provides concurrency in various queue operations like retrieval, insertion, deletion, etc. The Queue waits to become non-empty at the time of retrieving any elements. BlockingQueue should not contain null elements. The implementation of this Queue is thread-safe. The syntax of BlockingQueue is:
10) Explain override equals() method
The equals method is used to check the similarity between two objects. In case if the programmer wants to check an object based on the property, then it needs to be overridden.
11) What is the difference between Comparable and Comparator?
The difference between Comparable and Comparator is:
12) Explain equals() with example
Equals() verifies whether the number object is equal to the object, which is passed as an argument or not. The syntax of the equals() method is: This method takes two parameters 1) any object, 2) return value. It returns true if the passed argument is not null and is an object of a similar type having the same numeric value. Example:
13) List out benefits of generic collection
The benefits of using the generic collection are:
If the programmers are using generic class, they don’t require typecasting. It is type-safe and can be checked at the time of compilation. It provides the stability of the code by detecting bug at the compilation time.
14) Explain the method to convert ArrayList to Array and Array to ArrayList
Programmers can convert an Array to ArrayList using asList() method of Arrays class. It is a static method of Arrays class that accept the List object. The syntax of asList() method is: Java programmers can convert ArrayList to the List object using syntax:
15) Give example of ArrayList
The Example of reverse ArrayList is:
16) Give example to sort an array in dscending order
The example of sort an array in decending order is:
17) Explain the basic interfaces of the Java collections framework
Java collection framework is a root of the collection hierarchy. It represents a group of objects as its elements. The Java programming language does not provide a direct implementation of such interface.
Set: Set is a collection having no duplicate elements. It uses hashtable for storing elements. List: List is an ordered collection that can contain duplicate elements. It enables developers to access any elements from its inbox. The list is like an array having a dynamic length. MAP: It is an object which maps keys to values. It cannot contain duplicate keys. Each key can be mapped to at least one value.
18) What are the features of Java Hashmap?
Features of Java Hashmap are:
The values can be stored in a map by forming a key-value pair. The value can be retrieved using the key by passing it to the correct method. If no element exists in the Map, it will throw a ‘NoSuchElementException’. HashMap stores only object references. That is why it is impossible to use primitive data types like double or int. Use wrapper class (like Integer or Double) instead.
19) What is a Stack?
A stack is a special area of computer’s memory that stores temporary variables created by a function. In stack, variables are declared, stored, and initialized during runtime.
20) What is linked list?
A linked list is a data structure that can store a collection of items. In other words, linked lists can be utilized to store several objects of the same type. Each unit or element of the list is referred as a node. A node in the Linked list has its data and the address of the next node. It is like a chain. Linked Lists are used to create graphs and trees.
21) Give example of ArrayList
The example of ArrayList is:
22) Explain linked list supported by Java
Two types of linked list supported by Java are:
Singly Linked list: Singly Linked list is a type of data structure. In a singly linked list, each node in the list stores the contents of the node and a reference or pointer to the next node in the list. It does not store any reference or pointer to the previous node. Doubly linked lists: Doubly linked lists are a special type of linked list wherein traversal across the data elements can be done in both directions. This is made possible by having two links in every node, one that links to the next node and another one that connects to the previous node.
23) Explain the methods provided by the Queue interface?
Methods of Java Queue interface are:
24) Mention the methods provided by Stack class
Important methods provided by Stack class are:
push(): Push item into the stack. empty (): This method finds that whether the stack is empty or not. pop (): This Java collection framework method removes the object from the stack. search (): This method searches items in the stack. peek (): This Java method looks at the stack object without removing it.
25) Define emptySet() in the Java collections framework
Method emptySet() that returns the empty immutable set whenever programmers try to remove null elements. The set which is returned by emptySet() is serializable. The syntax of this method is:
public static final
26) Differentiate between Collection and Collections
The difference between Collection and Collections are:
27) Define LinkedHashSet in the Java Collection framework?
LinkedHashSet is a subclass of the class called HashSet and implements the set interface. It is a well-ordered version of HashSet that maintains a doubly-linked List across its all elements.
28) What is the difference between failfast and failsafe?
29) List collection views of a map interface
Collection views of map interface are: 1) key set view, 2) value set view, and 3) entry set view.
30) What are the benefits of the Collection Framework in Java?
The benefits of Collection Framework in Java are:
Java collection framework offers highly efficient and effective data structures that enhance the accuracy and speed of the program. The program developed with the Java collection framework is easy to maintain. A developer can mix classes with other types that result in increasing the reusability of code. The Java collection framework enables programmers to modify the primitive collection types the way they like.
31) What is a good way to sort the Collection objects in Java?
A good way to sort Java collection objects is using Comparable and Comparator interfaces. A developer can use Collections.sort(), the elements are sorted based on the order mention in compareTo(). When a developer uses Collections, sort (Comparator), it sorts the objects depend on compare() of the Comparator interface.
32) Explain Vector in Java
The vector is the same as an array. It has components that can be accessed using an index value. Vectors can contain a legacy method that is nor part of the collection framework.
33) What is the difference between Set and Map?
34) Define dictionary class
The Dictionary class is a Java class that has a capability to store key-value pairs.
35) Define EnumSet
java.util.EnumSet is Set implementation that can be used with enum types. EnumSet having all elements must come from one enum type specified explicitly or implicitly. It is not synchronized, and also null keys are not allowed. EnumSet provides methods like EnumSetof(E first, E… rest), complementOf(EnumSet s), and copyOf(Collection c).
36) What are the two ways to remove duplicates from ArrayList?
Two ways to remove duplicates from ArrayList are:
HashSet: Developer can use HashSet to remove the duplicate element from the ArrayList. The drawback is it cannot preserve the insertion order. LinkedHashSet: Developers can also maintain the order of insertion by using LinkedHashSet instead of HashSet.
37) What is IdentityHashMap?
IdentityHashMap is a class that implements Serializable, Clonable interfaces, Map, and extends AbstractMap class. It is designed for the case wherein there is a need of reference-equality semantics.
38) What is WeakHashMap?
WeakHashMap is an implementation of the Java Map. It is used to store weak references to its keys. Sorting using this Map allows a key-value pair is collected as garbage. Its key is not referenced outside WeakHashMap.
39) What are the methods to make collection thread-safe?
The methods to make collection thread safe are:
Collections.synchronizedList(list); Collections.synchronizedMap(map); Collections.synchronizedSet(set);
40) Explain UnsupportedOperationException
UnsupportedOperationException is an exception whch is thrown on methods that are not supported by actual collection type. For example, Developer is making a read-only list using “Collections.unmodifiableList(list)” and calling call(), add() or remove() method. It should clearly throw UnsupportedOperationException.
41) Name the collection classes that gives random element access to its elements
Collection classes that give random element access to its elements are: 1) ArrayList, 2) HashMap, 3) TreeMap, and 4) Hashtable.
42) Explain the difference between Queue and Deque.
43) Mention the implementing List and Set interface
Class implementing List interface: 1) ArrayList, 2) Vector, and 3) LinkedList. Class implementing Set interface: 1) HashSet, and 2) TreeSet.
44) Explain the design pattern followed by Iterator
The iterator follows the detail of the iterator design pattern. It provides developer to navigate through the objects collections using a common interface without knowing its implementation.
45) What is the peek() of the Queue interface?
Peek () is a method of queue interface. It retrieves all the elements but does not remove the queue head. In case if the Queue is empty, then this method will return null.
46) What is CopyOnWriteArrayList?
CopyOnWriteArrayList is a variant of ArrayList in which operations like add and set are implemented by creating a copy of the array. It is a thread-safe, and thereby it does not throw ConcurrentModificationException. This ArrayLists permits all the elements, including null.
47) Differentiate between ArrayList and LinkedList
The difference between ArrayList and LinkedList is:
48) Explain the methods of iterator interface
Methods of iterator interface are:
49) What are the methods of the HashSet class?
Methods of HashSet class are:
50) What are the methods of Java TreeSet class?
The methods of Java TreeSet class are:
51) Explain Linked HashSet
Java LinkedHashSet class is a Linked list and Hash table implementation of the Set interface. It contains unique elements same as a HashSet. Linked HashSet in Java also provides optional set operations that can maintain the order of insertion.
52) What are the important methods used in a linked list?
The important methods used in the linked list are:
53) List various classes available in sets
Various classes available in sets are: HashSet, TreeSetand, and LinkedHashSet.
54) List methods available in Java Queue interface
boolean add(object) boolean offer(object) object remove() object poll() object element() object peek()
55) Differentiate between List and Set.
56) Explain for each loop with example
For-Each Loop is another form of for loop used to traverse the array. It reduces the code significantly, and there is no use of the index or rather the counter in the loop. Exmple of for each loop:
57) Explain diamond operator
Diamond operator enables the compiler to collect the type arguments of generic class. In Java SE, developer can substitute the parameterized constructor with an empty parameter sets (<>) known as diamond operator.
58) Explain randomaccess interface
RandomAccess interface is used by List implementations for the indication that they are supporting fast.
59) Name the collection classes that implement random access interface
Java.util package has classes that can implement random access interface are: CopyOnWriteArrayList, Stack, ArrayList, and Vector.
60) How to join multiple ArrayLists?
The list provides a addall() method multiple ArrayList in Java. For example, consider two lists 1) areaList and 2) secondAreaList. A developer can join them using addall() like: areaList.addAll(secondAreaList);
61) Explain deque Interface
Java.util.Deque is Java, an interface that extends Queue interface. It gives support for the insertion and deletion of elements at both the end. This Queue is also called a double-ended queue.
62) Explain Linkedhashmap
LinkedHashMap is the implementation of the Map interface. It can also extends the HashMap class. Therefore, like HashMap, LinkedHashMap enables Java developers to allow one null key and more than one null value.
63) Explain methods to remove elements from ArrayList
The methods to remove elements from ArrayList are:
64) Explain map. entry In Map
Map.entry is a Java interface of java.util. It has a nested interface in Map. This interface must be qualified by the name of class or interface, which it is a member. Therefore it is qualified as a Map. Entry. It represents a key and value pair that can forms element of a Map. This method returns a view of the collection. For example, consider cityMap as a map. The developer can use entrySet() to get the set view of map having an element Map.Entry. Programmer can also use getKey() and getValue() of the Map.Entry to get the pair of key and value of the map.
65) Which method is used to sort an array in ascending order?
Java collection framework method, Collections.sort() is used to sort an array in ascending order.
66) How to measure the performance of an ArrayList?
The performance of ArrayList can be measure by:
Adding an element: Developer can add an element at the end of ArrayList using add(E e) method. It is O(1). In the worst scenario, it might go to O(n). This can happen if the developer add more elements than the array capacity. Retrieving an element: Developer can access the array index using get(int index). The performance, in this case, can be measure using ArrayList get() is O(1). Removing an element: In case, if the developers are removing element using the remove(int index), then the performance of ArrayList can be calculated using said remove(int index) operation is O(n – index) method.
67) Explain LinkedList class
LinkedList class in Java implements Deque and List using a doubly linked list. There is a private class node in a doubly-linked list which provides its structure. It also has an item variable for holding the value and reference to Node class. This can be used for connecting the next and previous nodes.
68) Give an example of Hashmap
The example of Hashmap is:
69) How to iterate map?
The developer cannot directly iterate map, but, this interface has two methods that gives view set of map. These methods are:
Set<Map.Entry<K, V»entrySet(): It is a method that returns a set having the entries mention in the map. These entries are generally objected, which has type Map. Entry.
Set
70) Explain Treemap in Java
TreeMap is a class that implements the Map interface LinkedHashMap and HashMap. It can also implements the NavigableMap interface and can extends the AbstractMap class.
71) What is the difference between Hashmap and Hashtable?
72) Explain the internal working of HashSet in Java
HashSet in Java internally uses HashMap to store elements. It can also store unique values with no duplicate values. In Java, HashSet developer can have add(E e) method that takes just the element to add as a parameter. It does not accept the key and value pair.
73) Explain Big-O notation with an example
The Big-O notation depicts the performance of an algorithm as the number of elements in ArrayList. A developer can use Big-O notation to choose the collection implementation. It is based on performance, time, and memory. For example, ArrayList get(index i) is a method to perform a constant-time operation. It does not depend on the total number of elements available in the list. Therefore, the performance in Big-O notation is O(1).
74) Explain the best practices in Java Collection Framework
The best practices in Java Collection Framework are:
Choose the correct type of collection depends on the need. Avoid rehashing or resizing by estimating the total number of elements to be stored in collection classes. Write a Java program in terms of interfaces. This will help the developer to change it’s implementation effortlessly in the future. A developer can use Generics for type-safety. Use immutable classes given by the Java Development Kit. Avoid implementation of equals() and hashCode() for custom classes. A programmer should use the Collections utility class for algorithms or to get read-only, synchronized, or empty collections. This will enhance code reusability with low maintainability.
75) Explain various types of queues in Java
There are three types of queues in Java:
Priority queue: It is a special type of Queue wherein elements are sorted as per their natural ordering or custom comparator. Circular Queue: It is a type of Queue in which user operations are performed based on the FIFO method. The last element is connected to the first position in order to make a circle. Double-ended Queue: A double-ended queue is an abstract data type that generalizes a queue. The elements in this queue can be added or removed from either head or tail.
76) What is the difference between stack and Queue?
77) What is the difference between array and stack?
The difference between array and stack is:
78) Define Iterator()
The Iterator() is an interface that provides methods to iterate Collection. Iterator can take the place of Enumeration in Java. It allows the caller to remove elements from the collection. The method provides a generic way for traversal using elements of the collection and implementing iterator design pattern.
79) What are the various ways to iterate over a list?
Java collection Framework programmer can iterate over a list in two ways: 1) Using iterator, and 2) using it for each loop.
80) What are the advantages of the stack?
The advantages of the stack are:
It helps you to manage the data in a Last In First Out (LIFO) method, which is not possible with the Linked list and array. When a function is called, the local variables are stored in a stack, and it is automatically destroyed once returned. A stack is used when a variable is not used outside that function. It allows you to control how memory is allocated and deallocated. Stack automatically cleans up the object. Not easily corrupted Variables cannot be resized.
These interview questions will also help in your viva(orals)