β˜• Senior Java Engineer Interview Prep

Interview Questions

150 multiple-choice questions across 5 topics. Select your answer, check it, and read the explanation.

1
Medium

A UserService class handles registration, sends emails, generates reports, and manages authentication. Which SOLID principle does this violate?

2
Medium

You have a DiscountCalculator with a switch statement that checks order type (PREMIUM, SEASONAL, LOYALTY). Adding a new discount type requires modifying this class. Which principle is violated and what pattern fixes it?

3
Hard

class Square extends Rectangle. Rectangle has independent setWidth() and setHeight(). Square overrides both to keep width == height. A test calls setWidth(5), setHeight(10), and expects area == 50. Square returns 100. Which principle is violated?

4
Medium

An interface UserOperations has methods: findById(), findAll(), save(), delete(), sendEmail(), generateReport(), exportToCsv(). A read-only REST controller must implement all methods. Which principle is violated?

5
Medium

public class OrderService { private MySQLOrderRepository repo = new MySQLOrderRepository(); }. Switching to PostgreSQL requires modifying OrderService. Which principle is violated?

6
Medium

In a Spring Boot app, you have @Service public class PaymentService that directly instantiates new StripeClient(). To test PaymentService, you must have a live Stripe connection. Which SOLID principle primarily solves this?

7
Hard

You refactor a God class into 47 tiny classes, each with a single method. Code is now hard to navigate and understand. What went wrong?

8
Medium

A reporting module has ReportGenerator that creates PDF, CSV, and Excel reports via if-else branches. You want to add HTML reports without touching ReportGenerator. Which approach best applies OCP?

9
Hard

Your team argues: "We use Spring @Autowired everywhere, so we already follow DIP." Is this correct?

10
Hard

A base class Bird has fly(). Penguin extends Bird but throws UnsupportedOperationException in fly(). Code iterating List<Bird> and calling fly() crashes. Fix?

11
Expert

You inherit a legacy monolith where all SOLID principles are violated. Which principle do you fix FIRST and why?

12
Hard

In CQRS architecture, you have separate ReadModel and WriteModel. Which SOLID principle most directly justifies this separation?

13
Expert

A service implements Closeable and has a close() method. A subclass overrides close() to do nothing (resources are managed elsewhere). Is this an LSP violation?

14
Medium

Spring Data JPA's CrudRepository<T,ID> has save(), findById(), findAll(), delete(), count(), existsById(). A service only needs findById(). Which principle suggests you define a custom interface?

15
Medium

You have a NotificationService that sends emails, SMS, and push notifications. Each channel has different retry logic, rate limits, and templates. Applying SRP, how should you restructure?

16
Expert

A method returns List<? extends Number>. Callers try to add elements and get compile errors. Which principle is being enforced by the compiler?

17
Hard

Your microservice architecture has OrderService directly calling InventoryService and PaymentService via HTTP. When InventoryService is down, OrderService fails. Which SOLID principle was ignored at the architecture level?

18
Medium

A class FileProcessor has methods: readFile(), parseCSV(), parseJSON(), parseXML(), writeToDatabase(), sendEmail(). How many SRP violations are present?

19
Expert

You apply OCP to a validator chain: List<Validator> validators. Adding new validation = new class. But now you realize the ORDER of validators matters. Does this break OCP?

20
Medium

You define interface Animal with eat(), sleep(), fly(), swim(). Dog implements Animal and must stub fly() and swim(). Which principle is violated?

21
Hard

In a Spring Boot app, you use @Profile("test") to swap MySQLRepo with InMemoryRepo, both implementing the same interface. Which SOLID principle enables this?

22
Hard

A team says "We follow SRP β€” each microservice has one responsibility." But their UserService handles user CRUD, authentication, session management, and password reset. Is their claim valid?

23
Expert

You have a method that accepts a Comparator<Object>. You pass a Comparator<String>. The compiler rejects it. This is related to which principle?

24
Hard

Your application has a Logger class used by every service. Logger handles console output, file writing, and sending to ELK. Any logging infrastructure change risks breaking all services. What's the SOLID fix?

25
Expert

A checked exception hierarchy: ServiceException β†’ DatabaseException β†’ ConnectionTimeoutException. A method declares throws ServiceException but the caller only handles ConnectionTimeoutException specifically. Is this an LSP concern?

26
Hard

You use the Strangler Fig pattern to migrate a monolith. New features follow SOLID; old code is wrapped behind interfaces and gradually replaced. This primarily leverages which principle?

27
Medium

A Spring Boot @RestController has @GetMapping, @PostMapping, @PutMapping, @DeleteMapping, plus methods for sending emails and generating PDFs. An SRP-compliant refactoring is:

28
Hard

You refactored to follow DIP: all services depend on interfaces. But your Spring Boot app now has 200+ interfaces, each with exactly one implementation. Is this a good application of DIP?

29
Expert

You measure a class's LCOM (Lack of Cohesion of Methods) score and it's very high, meaning methods use different subsets of instance variables. This indicates violation of:

30
Expert

In hexagonal architecture (ports and adapters), the domain layer defines ports (interfaces) and adapters implement them. Which SOLID principles does this enforce?