Senior Java Developer Interview Question Series-20(Questions Asked from Service-Based Companies)

Ajay Rathod
6 min readJun 9, 2023

--

In this article, I will be posting Java Developer Interview transcripts of Service-based companies, These sets of questions are asked to 8+ years of experience candidates. Let's dive into this,

(A bit of context: I’ve conducted numerous interviews for software engineering positions in most of the MNCs. Additionally, there have been instances where I faced challenges during coding interviews due to lack of preparation.)

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

Find my books: 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]

How to upgrade from Java 8 to Java 17?

Since the candidate was experienced, Interviewer started with how will you approach the Migration old Backend server from Java8 to java17.

Java has officially ended its support for Java 8 and they will provide support to Java 17 till 2029, so it makes sense to migrate to a newer version with support.

I have already discussed this here you can check below,

Container vs virtual machine

Containers:

  • Containers are lightweight, isolated environments that package an application and its dependencies together.
  • They share the host operating system’s kernel and resources, making them more efficient in terms of resource usage and startup time.
  • Containers are highly portable, allowing applications to run consistently across different environments.
  • They offer rapid deployment and scaling, making them suitable for microservices architectures and cloud-native applications.
  • Containers provide less isolation between applications compared to VMs, as they share the underlying operating system.

Virtual Machines:

  • VMs are complete virtualized instances of an operating system running on a physical host machine.
  • They allocate dedicated resources, including memory, CPU, and storage, to each virtual machine.
  • VMs provide strong isolation between applications since each VM runs its own operating system.
  • They offer flexibility in terms of running different operating systems and software stacks on the same physical machine.
  • VMs have a higher overhead due to the need for running multiple operating systems.

Solid principles

  1. Single Responsibility Principle (SRP): A class should have only one reason to change, meaning it should have a single responsibility or purpose.
  2. Open/Closed Principle (OCP): Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification, allowing for adding new functionality without modifying existing code.
  3. Liskov Substitution Principle (LSP): Objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program.
  4. Interface Segregation Principle (ISP): Clients should not be forced to depend on interfaces they do not use. It promotes designing fine-grained interfaces that are specific to the needs of the clients.
  5. Dependency Inversion Principle (DIP): High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details; details should depend on abstractions.

How to compare the equality of two customized objects in Java?

To compare the equality of two customized objects in Java, you need to override the equals() method and the hashCode() method. Here's how you can do it:

  1. Override the equals() method:
  • In your custom class, override the equals() method inherited from the Object class.
  • Inside the equals() method, compare the fields of the two objects to determine if they are equal.
  • Return true if the fields are equal, and false otherwise.
  • Ensure to follow the general contract of the equals() method, which includes checking for null, comparing types, and comparing individual fields.
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
CustomObject other = (CustomObject) obj;
// Compare fields of both objects for equality
// Return true if all fields are equal, false otherwise
}

2. Override the hashCode() method:

  • In your custom class, override the hashCode() method inherited from the Object class.
  • Calculate a hash code based on the fields used in the equals() method.
  • Ensure that objects that are considered equal by the equals() method produce the same hash code.
  • It’s important to follow the contract that equal objects must have equal hash codes.
@Override
public int hashCode() {
// Calculate hash code based on the fields used in equals()
// Ensure equal objects produce the same hash code
}

By overriding these methods, you can define how equality is determined for your customized objects. This allows you to compare instances of your class using methods like equals(), ==, and hashCode() as needed.

Which one is better Setter injection vs constructer injection

Both setter injection and constructor injection are dependency injection techniques used in object-oriented programming. The choice between the two depends on the specific requirements and design considerations of your application. Here are some considerations for each:

Constructor Injection:

  • Constructor injection involves passing dependencies as arguments to a class’s constructor.
  • Dependencies are explicitly defined and required at the time of object creation.
  • Constructor injection enforces the principle of “dependency first,” ensuring that a class is fully initialized with its dependencies before it can be used.
  • It promotes immutability by allowing dependencies to be set only once during object creation.
  • It can result in more straightforward and less error-prone code, as the required dependencies are clearly stated and cannot be changed after object creation.

Setter Injection:

  • Setter injection involves providing setter methods in a class to set its dependencies.
  • Dependencies can be set at any time after object creation, allowing for more flexibility.
  • Setter injection allows for optional dependencies or scenarios where dependencies may change during the object’s lifecycle.
  • It may lead to more mutable code, as dependencies can be modified after object creation.
  • Setter injection can be useful for frameworks that require a default constructor or for scenarios where circular dependencies exist.

Difference between map and flat map

  • map is used to transform each element of a collection independently and returns a new collection with the transformed elements.
  • flatMap is used to transform each element of a collection into a sequence of elements and then flatten the sequences into a single collection.

In other words, map applies a one-to-one transformation to each element, while flatMap applies a one-to-many transformation and flattens the resulting sequences into a single collection.

Iterate a hashmap using Java 8, in how many ways we can do it

  1. Using forEach and a lambda expression:
Map<KeyType, ValueType> map = new HashMap<>();

// Add some key-value pairs to the map

map.forEach((key, value) -> {
// Perform operations on each key-value pair
System.out.println("Key: " + key + ", Value: " + value);
});

2. Using the entrySet() method and a forEach loop:

Map<KeyType, ValueType> map = new HashMap<>();

// Add key-value pairs to the map

for (Map.Entry<KeyType, ValueType> entry : map.entrySet()) {
KeyType key = entry.getKey();
ValueType value = entry.getValue();

// Perform operations on each key-value pair
System.out.println("Key: " + key + ", Value: " + value);
}

3. Using the keySet() method and a forEach loop:

Map<KeyType, ValueType> map = new HashMap<>();

// Add key-value pairs to the map

for (KeyType key : map.keySet()) {
ValueType value = map.get(key);

// Perform operations on each key-value pair
System.out.println("Key: " + key + ", Value: " + value);
}

Find duplicate in list java 8 from list of strings?

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class Main {
public static void main(String[] args) {
List<String> strings = Arrays.asList("apple", "banana", "apple", "orange", "banana", "grape", "orange");

List<String> duplicates = strings.stream()
.collect(Collectors.groupingBy(s -> s))
.entrySet()
.stream()
.filter(entry -> entry.getValue().size() > 1)
.map(entry -> entry.getKey())
.collect(Collectors.toList());

System.out.println("Duplicates: " + duplicates);
}
}

Free PDF’s from @javinpaul from @javarevisited

Looking for Free Java, SpringBoot, or Microservice Interview Questions, Use the below Link to download the free pdf.

Grokking the Java Interview [Free Sample Copy]

Grokking the Spring Boot Interview [Free Sample Copy]

Thanks for reading

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

Find my books:

--

--

Ajay Rathod
Ajay Rathod

Written by Ajay Rathod

Java Programmer | AWS Certified | Writer | Find My Books on Java Interview here - https://rathodajay10.gumroad.com | YouTube - https://www.youtube.com/@ajtheory

Responses (1)