Senior Java Dev Interview Question 2023-series-17(Contains All the Interview questions till now)

In this article, we will explore some common interview questions frequently asked during senior Java developer interviews. These questions cover a broad range of topics, including core Java concepts, object-oriented programming principles, design patterns, spring-boot, Hibernate JPA, and more. By familiarizing yourself with these questions and their potential answers, you can better prepare for your next senior Java developer interview and showcase your expertise in the field.

Ajay Rathod
7 min readMay 16, 2023

--

(A bit of context: I’ve conducted numerous interviews for software engineering positions in most of the MNCs.)

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]

pexel

Below is the actual interview question transcript asked for a senior Java developer in May 2023, Even with the good experience we have to answer the basic questions.

What is String in Java

In Java, a String is a class that represents a sequence of characters. It is widely used to store and manipulate textual data in Java programs

How String is stored in java

String Pool:

The string pool is a special area of memory where Java stores string literals. String literals are string constants that appear directly in the source code of a Java program.

When a string literal is encountered, Java checks if the same string value already exists in the string pool. If it does, the existing string object is returned instead of creating a new one.

Heap Memory:

Strings that are not string literals (created using the new keyword or concatenation) are stored in the heap memory.

When a new string object is created using the new keyword, memory is allocated in the heap to store the string and its associated metadata.

Unlike string literals in the string pool, these objects are not shared and can have distinct references.

How Hashmap Works in Java

What is the default capacity of hashmap in Java

The default capacity of a HashMap in Java is 16. When you create a HashMap using the default constructor (new HashMap<>()), it initializes with this default capacity.

Can we create a custom hashmap in Java, if we add the initial capacity as 50 in hashmap will it create 50 buckets

Yes !! Creating a HashMap with an appropriate initial capacity can help optimize memory usage and reduce the number of resize operations, especially if you have an estimate of the number of entries you expect to store in the HashMap,

In the below example, we can provide an argument in the constructor for initial capacity.

HashMap<String, Integer> map = new HashMap<>(50);      

What is the exception hierarchy in Java

What is the Runtime exception, How to create custom runtime exception

They represent exceptional conditions that typically occur due to programming errors or unexpected runtime conditions

To create a custom runtime exception, you can define a class that extends RuntimeException or one of its subclasses. Here's an example of creating a custom runtime exception called CustomRuntimeException:

public class CustomRuntimeException extends RuntimeException {
public CustomRuntimeException() {
super();
}

public CustomRuntimeException(String message) {
super(message);
}

public CustomRuntimeException(String message, Throwable cause) {
super(message, cause);
}

public CustomRuntimeException(Throwable cause) {
super(cause);
}
}

How to process unique records in Java stream API, how to avoid duplicate record

Using distinct Stream api functions we can avoid processing duplicates,

import java.util.Arrays;
import java.util.List;

public class Example {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 3, 2, 5, 6, 4, 7, 8, 7, 6);

numbers.stream()
.distinct()
.forEach(System.out::println);
}
}

Diffrence between map vs flat map

Map:

  • The map() operation applies a function to each element in the stream and returns a new stream consisting of the results.
  • It performs a one-to-one mapping, where each input element is transformed into exactly one output element.
  • The resulting stream has the same size as the input stream.
List<String> names = Arrays.asList("John", "Alice", "Bob");
List<String> upperCaseNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
// Result: ["JOHN", "ALICE", "BOB"]

FlatMap:

  • The FlatMap() operation is used when each input element can be mapped to zero or more output elements.
  • It flattens nested collections or arrays into a single stream, merging the elements from all the nested collections or arrays.
  • The resulting stream may have a different size than the input stream, as the number of elements can vary based on the mapping
List<List<Integer>> numbers = Arrays.asList(
Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6),
Arrays.asList(7, 8, 9)
);
List<Integer> flattenedNumbers = numbers.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
// Result: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Hibernate load vs get

load():

  • The load() the method is a lazy-loading method. It returns a proxy object that acts as a placeholder for the actual entity and defers loading the data from the database until it's accessed.
  • If the entity is not found in the database, a special proxy object is returned (a subclass of the entity class) that throws an exception if any properties other than the identifier are accessed before initialization.
  • It can be useful when you only need the entity to access its identifier or establish associations without actually loading the complete entity data from the database.
Book book = session.load(Book.class, 123); // Returns a proxy object
System.out.println(book.getId()); // Does not trigger a database query

get:

  • The get() method eagerly loads the complete entity data from the database immediately upon invocation. It returns the actual entity object if found in the database or null if not found.
  • If the entity is not found, it simply returns null without throwing an exception.
  • It is suitable when you need the complete entity data and are confident that the entity with the specified identifier exists in the database.
Book book = session.get(Book.class, 123); // Returns the actual entity or null
System.out.println(book.getTitle()); // Triggers a database query

How to create a custom transaction manager’ in Spring boot

We have to follow three steps:

1. Implement the PlatformTransactionManager interface:

  • Create a new class that implements the PlatformTransactionManager interface.
  • Override the necessary methods defined by the interface to provide the desired transaction management behavior.

2. Register the custom transaction manager in the Spring Boot configuration:

  • In your Spring Boot configuration class, define a bean of type PlatformTransactionManager and provide an instance of your custom transaction manager.

3. Use the custom transaction manager in your application:

  • With the custom transaction manager registered, you can now use it in your application by injecting it where transaction management is required.

what are the bean scopes? difference between singleton and Request bean scope?

Singleton: The default scope in Spring.

Prototype: Creates a new instance of the bean every time it is requested.

Request (Web-specific): Creates a new instance of the bean for each HTTP request in a web application.

Session (Web-specific): Creates a single instance of the bean per HTTP session.

Global Session (Web-specific): Similar to the Session scope, but creates a single instance per global HTTP session

Design pattern used by Spring framework

Spring framework includes Dependency Injection (DI) Pattern, Singleton Pattern, Factory Pattern, Proxy Pattern, Observer Pattern, and Strategy Pattern.

What is @spingbootapplication annotation?

It combines three important annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan.

How to use validation in Springboot

by combining the javax.validation annotations with the @Valid annotation and utilizing the validation framework provided by Spring

What is the load-balancing policy we are using for Kubernetes

The Round Robin policy ensures that the workload is distributed evenly among the pods, providing a basic form of load balancing

Additional load-balancing strategies, such as session affinity (sticky sessions) or more sophisticated algorithms like weighted round-robin, least connections, or IP Hash, can be implemented by utilizing these external components or custom configurations based on your specific requirements.

How dispatcher servlet works

Let me know if these questions are helping you.

Till now I have published 17 articles on Java Developer interview Questions, I am sure if you have gone through this, this will help you clear the Java backend role

Java Developer Interview Questions Series:

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

Senior Java Developer Interview Questions Series-2

Core-Java Developer Interview Questions Series-3

Crack your Java+Microservice Developer Interview with these Real Question & Answers Series-4 (Fintech company specific, Microservice/SQL)

Senior Java Developer Interview Questions series-5 (OOPs, Core Java 8, Spring, Coding,prep material)

13 New Java Developer Interview Question Series-6 (Microservices and Java Coding)

Coding Interview Question for Experienced Java Developer role series-7

Top Java Developer Interviews Questions and Answers-8 (coding questions, Core java, Spring, Microservices)

Most Important Java/Microservice Developer Interview Question Series-10 (Spring Boot, Microservices, Distributed System,)

Top Java/Microservice Developer Interview Questions asked in interviews series — 11(Java-8, Spring-Boot, Microservices)

Most Common Java Developer Interview Questions series-12(Core Java,OOP)

Recently Asked Interview Questions for Java Developers series-13(Download free pdfs of Java & Spring-Boot Interview)

Practical Coding Interview Questions on Spring Data-JPA and Java Stream API Series -14

Senior Java Developer Interview Questions series-15

Must Know Java 8 Stream Interview Questions for Java Developers series-16

Thanks for reading

  • 👏 Please clap for the story and subscribe 👉
  • 📰 Read more content on my Medium (30 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/