2018 Top Java + Spring Interview Questions

Every year, I train myself performing interviews. One never knows what might come along.

Some years, I participate on more interviews, and other years less.

The important aspects, of periodically participating on interviews, are:

  • Developing “thick skin”;
  • Getting to know what are the “trending” questions;
  • And just practicing having quick answers, for usual questions.

This year, at least for the companies I have tried out, the most common asked questions, for Java+Spring where:

1- SOLID principles.

SOLID is a set of design patterns in Object Oriented software development.
The important part here for the interview, is to remember and understand each principle:

Single Responsibility Principle
Open/Closed Principle
Liskov Substitution Principle
Interface Segregation Principle
Dependency Inversion

2- Describe a Singleton, and the different ways to write a Singleton class.

Singleton pattern, is one of the patterns discussed in the book Design Patterns: Elements of Reusable Object-Oriented Software (1994), and is the most common question asked for java developers.

In general, there is not just one way to write a Singleton, but the important in this question, is to understand when you should write a Singleton, and the basic ways to write it.

Basic rules:

  • Private constructor
  • Private static variable of the same class
  • Public static method, that returns the instance of the class

3- What design patterns do you know.

In this question, if you read the book Design Patterns: Elements of Reusable Object-Oriented Software (1994), you have an enormous advantage. But if you didn’t, don’t worry. The following are the 23 design patterns mentioned on the book:

Creational Patterns:

  • Abstract Factory:
  • Builder:
  • Factory Method:
  • Prototype:
  • Singleton:

Structural Patterns:

  • Adapter:
  • Bridge:
  • Composite:
  • Decorator:
  • Facade:
  • Flyweight:
  • Proxy:

Behavioral Patterns:

  • Chain of Responsibility:
  • Command:
  • Interpreter:
  • Iterator:
  • Mediator:
  • Memento:
  • Observer:
  • State:
  • Strategy:
  • Template Method:
  • Visitor:

4- AOP and Spring

Aspect Oriented Programming is a key component of Spring Framework.

Spring AOP modules provide interceptors for an application. When a method is executed, you can add additional functionality before or after the method execution.

Types of advice:

  • before
  • after
  • after-returning
  • after-throwing
  • around

5- What’s the difference between @Factory and @AbstractFactory

The Factory Method pattern uses inheritance and relies on a subclass to handle the desired object instantiation.

The Abstract Factory pattern, a class delegates the responsibility of object instantiation to another object via composition.

6- What’s the relation between Reflection and Frameworks such as Spring?

First, let’s understand the basic reflection concept. Reflection allows us to inspect or/and modify runtime attributes of classes, interfaces, enum, fields and methods. Very useful, when we don’t know their names at compile time.

Not only Spring, but most of J2EE frameworks, use Java Reflection. The following are some examples:

  • JUnit
  • Struts
  • Spring
  • Hibernate

And the list goes on, since those frameworks have no knowledge of the user defined classes, interfaces, methods, enums, etc…

7- What is dependency injection?

Dependency Injection (DI) is a concept used in many programming languages, and is based on the Inversion Of Control(Ioc). This concept states that a class should not configure or instantiate its dependencies, rather, the dependencies should be configured from the outside.
Classes should be as independent as possible from other classes.
In Java, one can use dependency injection through annotation such as @Inject and @Named, or using frameworks such as Spring, with the @Autowired annotation, as an example.

7.1- What ways can you inject a bean in Spring?

  • Spring Constructor Injection
  • Spring Setter Injection
  • Spring Field Injection

7.2 – What’s the best recommended way to use dependency injection in Spring?

The answer is: DEPENDS. There is no correct answer for this question. What the interviewer really wants to know, is whether you have the correct arguments, for each of the ways, one can inject a bean in Spring.

Some of the arguments are:

  • Readability
  • Immutability
  • State Safety
  • Too many instance fields

When we start to analyse all of the above, in general Field and Constructor injection, are the winners. Although at the end, it will be your choice, depending on your program, and the dependencies involved.

8- What Spring annotations do you know?

Spring has a lot of annotations, depending on the module you are working with. The following are the most common annotations you most probably work with, although there are much more.

  • Core Spring
    • @Required
    • @Autowired
    • @Qualifier
    • @Configuration
    • @ComponentScan
    • @Bean
    • @Lazy
    • @Value
  • Stereotype
    • @Component
    • @Controller
    • @Service
    • @Repository
  • SpringBoot
    • @EnableAutoConfiguration
    • @SpringBootApplication
  • SpringMVC and Rest
    • @Controller
    • @RestController
    • @RequestMapping
    • @CookieValue
    • @CrossOrigin
    • @GetMapping
    • @PostMapping
    • @PutMapping
    • @DeleteMapping
    • @ExceptionHandler
    • @PathVariable
    • @RequestAttribute
    • @RequestBody
    • @RequestHeader
    • @RequestParam
    • @ResponseBody
    • @ResponseStatus
    • @ControllerAdvice
    • @RestControllerAdvice
    • @SessionAttribute

9- Have you worked with Lambdas(Java 8)? Please describe.

What the interviewer wants from you with this question, is to know some basic concepts of Java 8. And one of the main changes about Java 8, is working with lambdas.

You can start describing the problems of working with anonymous classes, and the heavy syntax they leave on your code.

Lambda expressions let you express instances of single-method classes more compactly.

Lambda expressions are considered the first step to Java functional programming.

The following is an example of a Lambda expression, with zero parameters:

() -> System.out.println("Print something");

Note that it has a parenthesis, followed by the new “arrow” operator.

10- What are the OOP concepts in Java?

As straightforward as possible, the following are the concepts:

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance
  • Association
  • Aggregation
  • Composition

It is recommended that you study all of the above concepts.

That is it! The interviews are becoming more and more difficult, depending on the company you are applying to. So, besides studying language especifc paradigms, you need to study the most common interview questions, so you don’t get caught off guard.

Good luck!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s