Category: Java interview questions
-
What is the default capacity of collection framework classes in Java?
The answer to this questions is – it depends on the Java version. For example, in Java 8 default capacity of ArrayList is 0 until we add at least one object into the ArrayList object. But to answer this question as of Java 7, we mention following default capacity of collection framework classes in Java:
-
What is initial capacity, load factor and rehashing of a HashMap?
As per Java documentation: An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how…
-
Does HashMap allows null keys and null values?
HashMap allows one null key and multiple null values.
-
How to make an ArrayList read only in Java?
Sometimes there is a need to create a list which can not be modified. That is, list should not allow add, remove or set operations. Collections class provides unmodifiableCollection(Collection c) method to create unmodifiable collection. There are separate methods like unmodifiableList(List l) and unmodifiableSet(Set s) for creating unmodifiable list and set. One of the misconception to create read only ArrayList is to use Arrays.asList(T……
-
Why Map interface doesn’t extend Collection interface?
This is because the two interfaces have a very different semantics. Following points can be provided to support this:
-
What do you understand by fail-fast and fail-safe iterator in Java?
Fail-fast iterator It is generally not permissible for one thread to modify a collection while another thread is iterating over it. There are iterator implementations which throw ConcurrentModificationException in such scenario. This type of iterators are fail-fast iterators. All general purpose collection iterator implementations provided by JRE are fail-fast. Please note the following for ConcurrentModificationException,…
-
What is the difference between Iterator and ListIterator?
1. Introduction Iterator and ListIterator are two of the three cursors in Java. Third cursor is Enumeration. Iterators allow the caller to remove elements from the underlying collection during the iteration with well-defined semantics. Both Iterator and ListIterator are defined in java.util package.Iterator allows to traverse collection elements one by one and only in forward…
-
What is the benefit of Generics in Collections Framework?
In Java, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs. The difference is that the inputs to formal parameters are values, while…
-
What is an exception?
An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions. When an exception occurs within a method, the method creates an exception object and passes this to runtime system. This exception object contains information about the exception, like its type. Creating an exception…
-
Does an interface extends Object?
Consider the above code. We have defined an interface and have not provided any method in it. Since MyClass implements MyInterface, myInterface reference variable can be used to refer to a MyClass object. Thus, methods provided by interface can be invoked using variable myInterface. We can see from above code that interface has not defined any method, then the question comes to…
