Section 18 Β· 22 questions

OOP and SOLID

22 interview questions and answers in the OOP and SOLID section.

English OOP and SOLID Source Markdown
Language versions: English Russian Ukrainian

Questions in this section

  1. What is Single Responsibility Principle and How to Apply It
  2. Give an Example of Single Responsibility Principle Violation
  3. What is Open-Closed Principle?
  4. How to Refactor Code that Violates Open-Closed Principle?
  5. What is Liskov Substitution Principle?
  6. Give an Example of Liskov Substitution Principle Violation
  7. What is Interface Segregation Principle?
  8. What is Dependency Inversion Principle?
  9. Why Do We Need SOLID Principles at All?
  10. What is Composition and Inheritance
  11. When Is It Better to Use Composition Instead of Inheritance
  12. What is Delegation in OOP
  13. How is Single Responsibility Related to Cohesion
  14. What Happens if a Class Has Multiple Reasons to Change
  15. How Does SOLID Help in Code Testing?
  16. How is Dependency Inversion Related to Dependency Injection?
  17. What is Law of Demeter (Principle of Least Knowledge)?
  18. How to Refactor God Object?
  19. How Do SOLID Principles Help When Extending Functionality?
  20. Can You Follow All SOLID Principles at Once?
  21. How to Determine if a Class Has Single Responsibility?
  22. What Anti-patterns Contradict SOLID Principles?

Study navigator

22 questions for preparing for a Middle Java Developer interview.


πŸ“‹ All Questions

# Question Difficulty Level
1 What is Single Responsibility principle and how to apply it ⭐
2 Give an example of Single Responsibility principle violation ⭐⭐
3 What is Open Closed principle ⭐⭐
4 How to refactor code that violates Open Closed principle ⭐⭐⭐
5 What is Liskov Substitution principle ⭐⭐
6 Give an example of Liskov Substitution principle violation ⭐⭐⭐
7 What is Interface Segregation principle ⭐⭐
8 What is Dependency Inversion principle ⭐⭐
9 Why do we need SOLID principles at all ⭐⭐
10 What is composition and inheritance ⭐⭐
11 When is it better to use composition instead of inheritance ⭐⭐
12 What is delegation in OOP ⭐⭐
13 How is Single Responsibility principle related to cohesion ⭐⭐⭐
14 What happens if a class has multiple reasons to change ⭐⭐
15 How SOLID helps in code testing ⭐⭐
16 How Dependency Inversion principle is related to Dependency Injection ⭐⭐
17 What is Law of Demeter (principle of least knowledge) ⭐⭐
18 How to refactor God Object ⭐⭐⭐
19 How SOLID principles help when extending functionality ⭐⭐
20 Is it possible to follow all SOLID principles simultaneously ⭐⭐⭐
21 How to determine if a class has a single responsibility ⭐⭐
22 Which anti-patterns contradict SOLID principles ⭐⭐

πŸ—ΊοΈ Topic Dependency Map

                    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                    β”‚   SOLID BASICS (1-9)                     β”‚
                    β”‚   1-2. SRP (what + violation example)    β”‚
                    β”‚   3-4. OCP (what + refactoring)          β”‚
                    β”‚   5-6. LSP (what + violation example)    β”‚
                    β”‚   7. ISP                                 β”‚
                    β”‚   8. DIP                                 β”‚
                    β”‚   9. Why SOLID                           β”‚
                    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                       β”‚
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β–Ό                          β–Ό                          β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ COMPOSITION   β”‚        β”‚ DEEPER         β”‚        β”‚ PRACTICE           β”‚
    β”‚ AND INHERIT.  β”‚        β”‚ SRP/DIP        β”‚        β”‚ AND ANTI-PATTERNS  β”‚
    β”‚ (10-12)       β”‚        β”‚ (13-16)        β”‚        β”‚ (17-22)            β”‚
    β”‚ 10. Compositionβ”‚       β”‚ 13. SRP +      β”‚        β”‚ 17. Law of Demeter β”‚
    β”‚ 11. When whichβ”‚        β”‚     cohesion   β”‚        β”‚ 18. God Object     β”‚
    β”‚ 12. Delegat.  β”‚        β”‚ 14. Multiple   β”‚        β”‚ 19. Extending      β”‚
    β”‚               β”‚        β”‚     reasons    β”‚        β”‚ 20. All SOLID at onceβ”‚
    β”‚               β”‚        β”‚ 15. Testing    β”‚        β”‚ 21. How to determineβ”‚
    β”‚               β”‚        β”‚ 16. DIP vs DI  β”‚        β”‚ 22. Anti-patterns  β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🟒 Junior Level (weeks 1-2)

Step Topic Files Goal
1 SOLID overview Q9 Why we need principles
2 SRP Q1, Q2 Single responsibility, violation examples
3 OCP Q3 Open for extension, closed for modification
4 Composition vs inheritance Q10, Q11 Why composition is preferred
5 Delegation Q12 How delegation works

🟑 Middle Level (weeks 3-4)

Step Topic Files Goal
1 LSP Q5, Q6 Substitutability of subclasses, violation examples
2 ISP Q7 Don’t force implementing unnecessary methods
3 DIP Q8, Q16 Dependency on abstractions, relation to DI
4 SRP + Cohesion Q13 How SRP affects cohesion
5 Consequences of violation Q14, Q15 What happens with SRP violation, how SOLID helps testing
6 Law of Demeter Q17 Principle of least knowledge

πŸ”΄ Senior Level (weeks 5-6)

Step Topic Files Goal
1 OCP Refactoring Q4 (Senior) Strangler Fig, Strategy, Visitor, SPI
2 God Object Refactoring Q18 Progressive refactoring, Bounded Contexts
3 SOLID Trade-offs Q20 When SOLID conflicts with itself
4 Determining SRP Q21 Stakeholder analysis, cross-cutting concerns
5 Anti-patterns Q22 STUPID mnemonic, Speculative Generality
6 Extending Functionality Q19 YAGNI, Strangler Fig

πŸ”— Key Connections Between Topics

Topic: SOLID Principles

Q9 (Why SOLID) β†’ Q1 (SRP) β†’ Q3 (OCP) β†’ Q5 (LSP) β†’ Q7 (ISP) β†’ Q8 (DIP)
     ↓
Q13 (SRP+Cohesion) β†’ Q14 (Multiple reasons) β†’ Q15 (Testing)

Key connections:

  • Q9 ↔ Q22: SOLID protects against STUPID anti-patterns
  • Q1 ↔ Q13: SRP β†’ high cohesion β†’ lower LCOM
  • Q3 ↔ Q4: OCP concept β†’ refactoring to OCP
  • Q5 ↔ Q6: LSP theory β†’ concrete violation examples (Date, SavingsAccount)

Topic: Composition and Inheritance

Q10 (Composition vs inheritance) β†’ Q11 (When which) β†’ Q12 (Delegation)

Key connections:

  • Q10 ↔ Q11: Concept β†’ practical selection rules
  • Q11 ↔ Q8: Composition β†’ DIP (depend on interface, not implementation)
  • Q12 ↔ Q10: Delegation = mechanism of composition

Topic: Practice and Anti-patterns

Q17 (Law of Demeter) β†’ Q18 (God Object) β†’ Q22 (Anti-patterns)
     ↓                      ↓
Q19 (Extending) β†’ Q20 (All SOLID) β†’ Q21 (How to determine SRP)

Key connections:

  • Q17 ↔ Q18: Train Wreck β†’ God Object β†’ refactoring
  • Q18 ↔ Q22: God Object β€” one of the STUPID anti-patterns
  • Q19 ↔ Q4: Extending functionality β†’ OCP refactoring
  • Q20 ↔ Q9: SOLID trade-offs β†’ why we need them

πŸŽ“ Cheat Sheet: What to Know for Each Level

🟒 Junior

  • SRP: one reason to change = one stakeholder
  • OCP: add new classes, don’t modify old ones
  • Composition > inheritance: has-a is better than is-a
  • DIP: depend on interfaces, not implementations
  • ISP: don’t force implementing unnecessary methods

🟑 Middle

  • LSP: subclass must not strengthen preconditions or weaken postconditions
  • Cohesion: SRP β†’ high cohesion, LCOM metric
  • Law of Demeter: don’t talk to strangers (a.b.c() = Train Wreck)
  • DI vs DIP: DI is a mechanism, DIP is an architectural rule
  • Delegation: black box, only public interface

πŸ”΄ Senior

  • OCP refactoring: Strangler Fig, Strategy, Visitor, SPI
  • God Object refactoring: Progressive refactoring, Bounded Contexts, Saga
  • SOLID trade-offs: when principles conflict (SRP vs DIP, OCP vs YAGNI)
  • Megamorphic calls: 3+ interface implementations β†’ performance degradation
  • Sealed interface (Java 17+): pattern matching, compiler guarantees exhaustiveness
  • STUPID: Singleton, Tight Coupling, Premature Optimization, Indescriptive Naming, Duplication

πŸ“ File Format

Each file contains:

  • 🟒 Junior Level β€” basic understanding, simple analogies, examples
  • 🟑 Middle Level β€” internals, common mistakes, practical examples
  • πŸ”΄ Senior Level β€” deep dive, edge cases, production experience, monitoring
  • 🎯 Interview Cheat Sheet β€” key points, common questions, red flags, related topics