Preparing for java developer job interview? If yes, here’s the most asked top 10 JAVA interview questions to follow.

Most Asked JAVA Interview Questions

What are fail-fast and fail-safe iterators?

Fail-fast iterators operate directly on the collection itself. During iteration, fail-fast iterators fail as soon as they realize that the collection has been modified and will throw a ConcurrentModificationException. Some examples include ArrayListHashSet, and HashMap.

Fail-safe iterates operate on a cloned copy of the collection and therefore do not throw an exception if the collection is modified during iteration. Examples would include iterators returned by ConcurrentHasMap or CopyOnWriteArrayList.

The main distinction between fail-fast and fail-safe iterators is whether or not the collection can be modified while it is being iterated.

What is immutable object?

Immutable classes are Java classes whose objects can not be modified once created. Any modification in Immutable object result in new object.

What is the volatile keyword?

In Java, each thread has its own stack, including its own copy of variables it can access. When the thread is created, it copies the value of all accessible variables into its own stack. The volatile keyword basically says to the JVM “Warning, this variable may be modified in another Thread”.

In all versions of Java, the volatile keyword guarantees global ordering on reads and writes to a variable. This implies that every thread accessing a volatile field will read the variable’s current value instead of (potentially) using a cached value.

In Java 5 or later, volatile reads and writes establish a happens-before relationship, much like acquiring and releasing a mutex.

Using volatile may be faster than a lock, but it will not work in some situations. The range of situations in which volatile is effective was expanded in Java 5; in particular, double-checked locking now works correctly.

What is difference between StringBuffer and StringBuilder in Java?

StringBuffer is mutable means one can change the value of the object . The object created through StringBuffer is stored in the heap . StringBuffer  has the same methods as the StringBuilder , but each method in StringBuffer is synchronized that is StringBuffer is thread safe.

StringBuilder is same as the StringBuffer , that is it stores the object in heap and it can also be modified . The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread safe.
StringBuilder is fast as it is not thread safe.

Basic difference between ArrayList and Vector?

ArrayList does not define the increment size . Vector defines the increment size.

Compare the sleep() and wait() methods in Java

sleep() is a blocking operation that keeps a hold on the monitor / lock of the shared object for the specified number of milliseconds.

wait() on the other hand, simply pauses the thread until either (a) the specified number of milliseconds have elapsed or (b) it receives a desired notification from another thread (whichever is first), without keeping a hold on the monitor/lock of the shared object.

sleep() is most commonly used for polling, or to check for certain results, at a regular interval. wait() is generally used in multi-threaded applications.

Does not overriding hashcode() method has any performance implication?

Hashcode function will result in frequent collision in HashMap which eventually increase time for adding an object into Hash Map.

What is a singleton class? Give a practical example of its usage.

A singleton class in java can have only one instance and hence all its methods and variables belong to just one instance. Singleton class concept is useful for the situations when there is a need to limit the number of objects for a class.

The best example of singleton usage scenario is when there is a limit of having only one connection to a database due to some driver limitations or because of any licensing issues.

Can main() method in Java can return any data?

In java, main() method can’t return any data and hence, it’s always declared with a void return type.

What is multi-threading? Write two ways of implementing multi-threading in Java.

Multi threading is a programming concept to run multiple tasks in a concurrent manner within a single program. Threads share same process stack and running in parallel. It  helps  in performance improvement of any program.

Multi threaded applications can be developed in Java by using any of the following two methodologies:

1. By using Java.Lang.Runnable Interface. Classes implement this interface to enable multi threading. There is a Run() method in this interface which is implemented.

2. By writing a class that extend Java.Lang.Thread class.


Hope these most asked java interview questions will help you in your interview. Best of Luck..!!