Category: Java interview questions
-
How to avoid ConcurrentModificationException while iterating a collection?
ConcurrentModificationException is thrown if the collection is changed while a thread is traversing over it using iterator. ConcurrentModificationException can occur both in single threaded as well as multi-threaded programs. For example, following program will throw ConcurrentModificationException because we are trying to remove element from the list while iterating: Output To avoid ConcurrentModificationException Following example shows…
-
How can you access the current thread in Java?
Sometimes we need to know about the currently executing thread. For example, for debugging purposes we want information about current Thread. Current thread here means the currently executing thread object in Java. currentThread() method returns a reference to the currently executing thread object. In the following example, currently executing thread is ‘main’ thread. We can get the…
-
What are important methods of Java Exception Class?
Following are some important methods of Exception class: If getLocalizedMessage returns null, then just the class name is returned.
-
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…