30 interview questions to ask any Java developer

#dedicated teams

Java is a reigning king among the top programming languages in the world. Being simple, object-oriented, platform-independent, multithreaded, and secure — not to mention a natural pick for Android development — it has long been a favorite of both large-scale enterprises (Goldman Sachs, eBay, Google) and startups that crave the stability of a time-tested stack.

No wonder, then, that the market is overflooded by Java developers — who wouldn’t want to claim their piece of this huge and often lucrative pie? But the fact is, not all Java engineers are equally qualified and a few strategic questions can tell you everything you need to know.

To set you up for success, our stellar Java developer Artsem Silich helped us compile a comprehensive guide to help you choose the best talent on today’s market.

Note: ★ refers to question complexity.

Why is Java not 100% object-oriented?
View the answer

Java is not 100% object-oriented as it uses eight primitive data types — boolean, byte, char, int, float, double, long, and short — that are not objects.

List all methods of the object class.
View the answer

Equals(), hashCode(), getClass(), notify(), notifyAll(), wait(), clone(), finalize(), toString()

What is the contract between equals() and hashCode()?
View the answer

If two objects are equal according to the equals(Object) method, then calling the hashcode() method on each of the two objects must produce the same integer result.

What is the right way to implement equals() for class User below?
View the answer

Q: public class User {
private String name;
private Date birth;
private int housenumber;
private long phonenumber;
//getters & setters

A: @Override
public boolean equals(Object object) {
// Basic checks.
if (object == this) return true;
if (!(object instanceof User)) return false;

// Property checks.
User other = (User) object;
return Objects.equals(name,
&& Objects.equals(birth, other.birth)
&& (housenumber == other.housenumber)
&& (phonenumber == other.phonenumber);

What is the difference in usage getClass() method instead of instanceof in equals() implementation?
View the answer

The performance of instanceof and getClass() == ... is different as they are doing different things. instanceof tests whether the object reference on the left-hand side (LHS) is an instance of the type on the right-hand side (RHS) or some subtype, while getClass() == ... tests whether the types are identical.

How do you make class immutable?
View the answer

Declare the class as final so it can't be extended. Make all fields private so that direct access is not allowed. Don't provide setter methods for variables. Make all mutable fields final so that its value can be assigned only once.

What is the difference between Checked and Unchecked Exception in Java?
View the answer

Checked Exceptions should be handled in the code using try-catch blocks. Or you can use another method that throws a keyword to let the caller know about the checked exceptions that might be thrown from the method. Unchecked Exceptions are not required to be handled in the program or to mention them in the throws clause of the method.

What is the problem with the program below?
View the answer

Q: public class TestException {

public static void main(String[] args) {
try {
} catch (IOException e) {
} catch (FileNotFoundException e) {

public static void go() throws IOException, FileNotFoundException{


A: The program won’t compile because FileNotFoundException is subclass of IOException, so the catch block of FileNotFoundException is unreachable and you will get error message as “Unreachable catch block for FileNotFoundException. It is already handled by the catch block for IOException.”

How would you resolve an error like java.lang.OutOfMemoryError in Java?
View the answer

The first and easier way to solve OutOfMemoryError in Java is to increase the maximum heap size by using JVM options "-Xmx512M," which will immediately solve your OutOfMemoryError.

Here is an example of increasing the maximum heap size of JVM, noting that it’s better to keep the -Xmx to -Xms ratio either 1:1 or 1:1.5 if you are setting heap size in your java application export JVM_ARGS="-Xms1024m -Xmx1024m."

The second way to resolve OutOfMemoryError in Java is rather difficult and comes when you don't have much memory and even after the increase of maximum heap size, you are still getting java.lang.OutOfMemoryError. In this case, you’ll probably profile your application and look for a memory leak

What is the top-level interface for Java Collection Framework?
View the answer

It's Collection interface.

Name the most popular interfaces extended from Collection interface?
View the answer

Queue, Deque, Set, SortedSet, and List.

What is the most popular implementation of Queue interface in Java?
View the answer


What is the difference between List and Set?
View the answer

List is a type of ordered collection that maintains the elements in insertion order, while Set is a type of unordered collection in which elements are not maintained in any order. List allows duplicates while Set doesn't allow duplicate elements.

If I would like to frequently insert values in the middle of List, which implementation should be considered more performant?
View the answer

In a LinkedList, once you have the correct position, insertion costs O(1), while in an ArrayList it goes up to O(n) and all elements past the insertion point must be moved using System.arrayCopy(). But in LinkedList, you pay the cost of iterating from the beginning to the end every time. Naturally, that's O(n).

On the other hand, the list must be quite large before you'll actually witness the performance benefit of mid-list insertion. System.arraycopy is a blazing-fast operation and, on the other end, each insertion into a LinkedList requires the allocation of a node instance.

To sum it up, ArrayList is a better choice for 99% or more of real cases, and leveraging the narrow advantage of a LinkedList requires great care.

What System.arraycopy is used for in ArrayList<>?
View the answer

Java System.arraycopy() is a native static method to copy elements from the source array to the destination array. It’s used in ArrayList to move/copy elements from the source positions to the new ones during insertions/deletions of elements.

What is the difference between HashSet vs TreeSet?
View the answer

Set is a generic set of values with no duplicate elements. A TreeSet is a set where the elements are sorted. A HashSet is a set where the elements are not sorted or ordered, and is faster than a TreeSet.

Can a null element be added to a TreeSet or HashSet?
View the answer

There is no null check in add() method. HashMap allows one null key while TreeSet uses the same concept as HashSet for internal logic, but uses NavigableMap for storing the elements:

private transient NavigableMap m;

private static final Object PRESENT = new Object();

As NavigableMap is a subtype of SortedMap which does not allow null keys, TreeSet also does not support null keys. It will throw a NullPointerException if you try to add a null element in TreeSet.

When should you use Comparator and Comparable interfaces?
View the answer

An object should implement Comparable if that is the clear, natural way to sort the class, and anyone may sort the class this way. If, however, the sorting was an unusual use of the class, or the sorting only makes sense for a specific use case, then Comparator is a better option.

How would you design a good key for hashmap?
View the answer

The most important constraint is, you must be able to fetch the value object back in the future. Otherwise, there is no use of having such a data structure. If you understand the working of hashmap, you will find it largely depends on hashCode() and equals() method of Key objects.

So a good key object must provide the same hashCode() again and again, no matter how many times it is fetched. Similarly, the same keys must return true when compared with equals() method and different keys must return false. For this reason, immutable classes are considered the best candidate for HashMap keys.

What are the collisions in HashMap?
View the answer

A collision, or more specifically, a hash code collision in a HashMap, is a situation where two or more key objects produce the same final hash value and hence point to the same bucket location or array index. This scenario can occur because, according to the equals and hashCode contract, two unequal objects in Java can have the same hash code. It can also happen because of the finite size of the underlying array, that is, before resizing. The smaller this array, the higher the chances of collision.

What is the difference between HashMap and ConcurrentHashMap in Java?
View the answer

1. ConcurrentHashMap is thread-safe; only one thread can access the code at a time, whereas HashMap is not thread-safe.
2. ConcurrentHashMap does not allow keys to contain null values, whereas HashMap can contain one null key.

What are the different ways to traverse HashMap in Java?
View the answer

1. Using keySet() and iterator()
2. Using entrySet() and enhanced for loop
3. Using entrySet() and iterator()
4. Using keySet() and get() method

How will you measure the performance of HashMap?
View the answer

An instance of HashMap has two parameters that affects its performance: load factor and capacity.

The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the hash table is rehashed (that is, internal data structures are rebuilt) so that the hash table has approximately twice the number of buckets.

The capacity is the number of buckets in the hash table (HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls), and the initial capacity is simply the capacity at the time the hash table is created.

What happens if two keys have the same hashCode in HashMap?
View the answer

If two keys are the same (equals() returns true when you compare them), their hashCode() method must return the same number. If keys violate this, then keys that are equal might be stored in different buckets, and the hashmap would not be able to find key-value pairs (because it's going to look in the same bucket).

What is the difference between Process and Thread?
View the answer

Process is a self-contained execution environment, and it can be seen as a program or application, whereas Thread is a single task of execution within the process. Java runtime environment runs as a single process, which contains different classes and programs as processes.

Meanwhile, Thread can be called a lightweight process. Thread requires less resources to create and exists in the process, sharing its resources.

What is a volatile keyword in Java?
View the answer

When we use a volatile keyword with a variable, all the threads read its value directly from the memory and don’t cache it. This makes sure that the value read is the same as in the memory.

What is ThreadLocal?
View the answer

Java ThreadLocal is used to create thread-local variables. We know that all threads of an Object share its variables, so if the variable is not thread-safe, we can use synchronization...but if we want to avoid synchronization, we can use ThreadLocal variables.

Every thread has its own ThreadLocal variable and they can use its gets () and set() methods to get the default value or change it’s value local to Thread. ThreadLocal instances are typically private static fields in classes that wish to associate the state with a thread.

What is Deadlock? How do you analyze and avoid a deadlock situation?
View the answer

Deadlock is a programming situation where two or more threads are blocked forever. It arises with at least two threads and two or more resources. To analyze a deadlock, we need to look at the java thread dump of the application, then look out for the threads with state as BLOCKED and then the resources it’s waiting to lock. Every resource has a unique ID that we can use to find which thread is already holding the lock on the object. Avoid Nested Locks, Lock Only What is Required, and Avoid Waiting Indefinitely are common ways to avoid deadlock situation.

What is the JIT compiler in Java?
View the answer

JIT stands for Just-In-Time compiler in Java. It is a program that helps convert the Java bytecode into instructions that are sent directly to the processor. By default, the JIT compiler is enabled in Java and is activated whenever a Java method is invoked. The JIT compiler then compiles the bytecode of the invoked method into native machine code, compiling it “just in time” to execute. Once the method has been compiled, the JVM summons the compiled code of that method directly rather than interpreting it, which is why it is often responsible for the performance optimization of Java applications at the run time.

If you could redesign Java today, what is that one thing you will change?
View the answer

Everyone who works with Java has his own answer to the question, so ask it to reveal the developer’s creativity!

Carefully-vetted Java engineers will help you make the most of this powerful programming language and build a secure, well-tested solution that delights users in no time.

But don’t forget that the technical skills and knowledge of your Java developer are only one piece of the puzzle; their collaboration, communication, and willingness to act as one solid team are another and should be equally valued.

We hope that this article will help you choose the best candidate and we appreciate the opportunity to serve as a resource and advisor at any stage in your product pipeline. Good luck!

Let's get rolling
Drive your business, and get solutions,
not just technologies.
Have a project in mind?
We'll help you develop this idea into a great solution.
Give us a shout!