Senior Java Developer Questions [Part2]— Job Interview

Bayram EKER
6 min readFeb 19, 2023

In this article we will answer SENIOR JAVA DEVELOPER job interview questions.

Senior Java Developers

As a senior Java developer, you will have 5+ years of experience, a deep understanding of your business’s IT systems, and expertise on various projects. You will likely spend your work time:

  • Planning, consulting, and reviewing sophisticated Java projects
  • Overseeing work done by junior staff members
  • Working with vendors
  • Overseeing the financial side of development

We will answer:

  • What questions come up in a java developer job interview?
  • What should a java developer know ?
  • What are the critical questions in a job interview ?

Q-1: What is the volatile keyword in Java?

Answer

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.

The volatile keyword is also useful for 64-bit types like long and double since they are written in two operations. Without the volatile keyword you risk stale or invalid values.

One common example for using volatile is for a flag to terminate a thread. If you’ve started a thread, and you want to be able to safely interrupt it from a different thread, you can have the thread periodically check a flag (i.e., to stop it, set the flag to true). By making the flag volatile, you can ensure that the thread that is checking its value will see that it has been set to true without even having to use a synchronized block. For example:

public class Foo extends Thread {
private volatile boolean close = false;
public void run() {
while(!close) {
// do work
}
}
public void close() {
close = true;
// interrupt here if needed
}
}

Q-2: How would you create an immutable object?

Answer

An immutable object is one that cannot change after it’s constructive. It’s a key foundation of simple, reliable code. However, Java developers are sometimes reluctant to use immutable objects if they need to account for updates down the line.

Oracle offers four ways to create immutable objects:

1. Don’t provide “setter” methods — methods that modify fields or objects referred to by fields.

2. Make all fields final and private.

3. Don’t allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.

4. If the instance fields include references to mutable objects, don’t allow those objects to be changed:

  • Don’t provide methods that modify the mutable objects.
  • Don’t share references to the mutable objects. Never store references to external, mutable oaZ

This strategy is considered advanced and shows that you’re at the senior level of development.

Q-3: How to sort HashSet of integers?

Answer

This is a tricky question, as the Set is by default an unordered structure. So the answer is simply no unless we use an additional data structure.

Starting from this question, it’s possible to:

  1. Investigate the candidate’s knowledge of Java collections. TreeSet, LinkedHashSet, and List are only some of the possible solutions.
  2. Proceed further with HashSet’s internal checks. Why do you think Sets are unordered? Why is it called a HASH set?

Q-4: What is the Spring IoC Container?

Answer

The Spring container uses dependency injection (DI) to manage the components that make up an application. It is responsible for creating objects, wiring them together, configuring them, and managing their complete lifecycle.

There are two types of Spring IoC containers:

  • Bean Factor Container. Basic support for DI. This container is the simplest and is usually preferred when resources are limited, such as for mobile devices or applet-based applications.
  • Spring ApplicationContext Container. Enterprise-specific functionality. Examples include the ability to resolve textual messages from a properties file or the ability to publish application events to interested listeners.

Q-5: Does Spring Bean provide thread-safety?

Answer

In default mode, Spring beans are not thread-safe because the scope is a singleton. There will be only one instance per context, which means that having a class-level variable that can be updated by any thread will lead to inconsistent data.

However, the Spring bean scope can be altered to request, prototype, or session to achieve thread-safety. This is a design decision based on project requirements. Making this adjustment will impact performance.

Q-6: Describe an exception hierarchy

Answer

Exception hierarchy is must-have knowledge when it comes to using frameworks like Spring or Spring Boot. The vast majority of Java libraries define and document as precisely as possible an exception hierarchy for the various use cases solved.

This abstraction is useful in several ways: first, it’s possible to catch exceptions at various levels of the hierarchy, and second, it’s possible to further extend the family of exceptions with peculiar and ad-hoc use cases.

A further related question to ask regards the concept of “fault barriers,” which I encourage every senior Java developer to understand if they aren’t already familiar with the construct.

Q-7: How to handle huge data on Redis cache?

Answer

The candidate’s first reaction to this question should be, “Does it make sense to have huge data on Redis?” A distributed cache is built primarily to enhance the performance of a distributed microservice application. Together with the physical limit of the RAM available for the Redis host, there is a software limit on a single item of 512MB. To store a huge data load, we should be able to split it into chunks and retrieve results, for example by using the HGETALL command. Compression before storing the data might also be an option.

Generally speaking, Redis was not built to achieve this. Retrieving vast chunks of data has been indicated as one of the worst practices. A way better solution could be to store the data object in a standard NoSQL database.

The goal of this interview question is to check the previous experience of senior Java developer candidates, with a focus on how they propose new solutions instead of sticking to the plan.

Q-8: What does the GetMethod of HashMap do in Java?

Answer

HashMap is constructed using the hash table data structure. Basically, it employs the ‘hashCode ( )’ method to compute hash code to find the bucket location on the underlying array and the ‘equals ( )’ method to detect the object in the same bucket in case of a collision.

Q-9: Write a sample unit testing method for testing exception named as IndexOutOfBoundsException when working with ArrayList.

Answer

@Test(expected=IndexOutOfBoundsException.class) 

public void outOfBounds() {
new ArrayList<Object>().get(1);
}

Q-10: What is the difference between a factory and an abstract factory pattern?

Answer

Abstract Factory provides an extra layer of abstraction. Consider different factories each extended from an Abstract Factory and responsible for the creation of different hierarchies of objects based on the type of factory. E.g. AbstractFactory extended by AutomobileFactory, UserFactory, RoleFactory, etc. Each individual factory would create objects of that specific type.

I hope the article was helpful. Don’t forget to follow to read our latest articles.

--

--