Senior Java Developer Interview Questions Series-1 (Core Java)

Hello Developers, I will cover Java, Spring-Boot, Microservices-related interview questions, and possible answers. On popular demand, I am also posting answers as links or text. This will help you clear your Java interviews for almost 0–6 years candidates.

Ajay Rathod
6 min readJan 3, 2022

If You prefer reading and watching this on YouTube here is the link down below under 2 mins.

Are you preparing for a job interview as a Java developer?

Find my book Guide To Clear Java Developer Interview here Gumroad (PDF Format) and Amazon (Kindle eBook).

Guide To Clear Spring-Boot Microservice Interview here Gumroad (PDF Format) and Amazon (Kindle eBook).

Download the sample copy here: Guide To Clear Java Developer Interview[Free Sample Copy]

Guide To Clear Spring-Boot Microservice Interview[Free Sample Copy]

If You prefer reading and watching this on YouTube here is the link above

What is the difference between HashMap and HashTable, which one is synchronized?

What difference between Hashmap vs concurrent hashmap and the follow-up question on how they work internally etc?

How does hashmap work internally?

How to create a custom immutable class in Java?

It's a classic interview question that will have follow-up questions,

In order to create an immutable class, you should follow the below steps:

  1. Make your class final, so that no other classes can extend it.
  2. Make all your fields final, so that they’re initialized only once inside the constructor and never modified afterward.
  3. Don’t expose setter methods.
  4. When exposing methods that modify the state of the class, you must always return a new instance of the class.
  5. If the class holds a mutable object:
  • Inside the constructor, make sure to use a clone copy of the passed argument and never set your mutable field to the real instance passed through the constructor, this is to prevent the clients who pass the object from modifying it afterward.
  • Make sure to always return a clone copy of the field and never return the real object instance.
public final class ImmutableStudent {

private final int id;
private final String name;

public ImmutableStudent(int id, String name) {
this.name = name;
this.id = id;
}

public int getId() {
return id;
}

public String getName() {
return name;
}
}

What are the in-built immutable classes in Java?

  • String
  • Wrapper classes such as Integer, Long, Double, etc.
  • Immutable collection classes such as Collections.singletonMap() etc.
  • java.lang.StackTraceElement
  • Java enums (ideally they should be)
  • java.util.Locale
  • java.util.UUID

Difference between Aggregation vs composition?

The main difference between aggregation and composition is that aggregation is an association among two objects that have the “has a” relationship while the composition is a special type of aggregation that describes ownership.

How to create an immutable map in Java, that we can’t modify after adding the data?

There are various ways in which we can create an Immutable Map.

– Using Collections.unmodifiableMap()

– Using Map.of()

– Using Map.ofEntries()

– Using Map.copyOf()

Explain Throw, throws, and throwable keywords in Java.

Throw: The throw keyword in Java is used to explicitly throw an exception from a method or any block of code.

throw Instance

//Example:

throw new ArithmeticException("/ by zero");

Throws In Java :

The throw is also a keyword in java that is used in the method signature to indicate that this method may throw mentioned exceptions. The caller to such methods must handle the mentioned exceptions either using try-catch blocks or using the throws keyword. Below is the syntax for using the throws keyword.

Java. lang.Throwable Class

Throwable is a superclass for all types of errors and exceptions in java. This class is a member of java.lang package. Only instances of this class or its subclasses are thrown by the java virtual machine or by the throw statement. The only argument of catch block must be of this type or its subclasses. If you want to create your own customized exceptions, then your class must extend this class.

What is the new change in the Java memory model in java 8?

What is Static synchronization in Java?

If you make any static method synchronized, the lock will be on the class not on the object.

How to create a Thread Pool and how to use it in the database connection pool?

https://www.baeldung.com/thread-pool-java-and-guava

What is the life cycle of thread and in Thread Pool?

Explain the Diamond problem in Java and how to solve that.

In Java, the diamond problem is related to multiple inheritances.

Remember that Java does not support multiple inheritances because of the diamond problem.

this is the problem,

interface Interface1 {
default public void foo() { System.out.println("Interface1's foo"); }
}
interface Interface2 {
default public void foo() { System.out.println("Interface2's foo"); }
}
public class Diamond implements Interface1, Interface2 {
public static void main(String []args) {
new Diamond().foo();
}
}

Error:(9, 8) java: class Diamond inherits unrelated defaults for foo() from types Interface1 and Interface2

In this case, resolve the conflict manually by using the super keyword within the Diamond class to
explicitly mention which method definition to use:

public void foo() { Interface1.super.foo(); }

How to create Create custom class loader in Java?

Java 8 Interface changes?

from Java 8, we can have default methods, functional interfaces, and static methods in the interfaces,

For creating a default method in the Java interface, we need to use the “default” keyword with the method signature. For example,

public interface Interface1 {

void method1(String str);

default void log(String str){
System.out.println("I1 logging::"+str);
}
}

Notice that log(String str) is the default method in the Interface1. Now when a class will implement Interface1, it is not mandatory to provide the implementation for default methods of the interface. This feature will help us in extending interfaces with additional methods, all we need is to provide a default implementation.

Java Interface Static Method

Java interface static method is similar to the default method except that we can’t override them in the implementation classes. This feature helps us in avoiding undesired results in case of poor implementation in implementation classes.

Java Functional Interfaces

An interface with exactly one abstract method is known as a Functional Interface. A new annotation @FunctionalInterface has been introduced to mark an interface as a Functional Interface.

How to create an Object in Java? Explain Different ways to do it.

https://www.tutorialspoint.com/5-different-ways-to-create-objects-in-Java

How to restrict object creation in Java?

we can simply use a singleton design pattern to do that.

here are quick steps to do that,

  • Private constructor to restrict instantiation of the class from other classes.
  • The private static variable of the same class is the only instance of the class.
  • The public static method that returns the instance of the class, this is the global access point for the outer world to get the instance of the singleton class.

Thanks for reading, I have added possible answers to the question.

Thanks for reading

  • 👏 Please clap for the story and follow me 👉
  • 📰 Read more content on my Medium (21 stories on Java Developer interview)

Find my books here:

--

--

Ajay Rathod

Software Engineer @Cisco | Java Programmer | AWS Certified | Writer | Find My Books on Java Interview here - https://rathodajay10.gumroad.com/