Interview Q&A / Object-Oriented Programming

INTERVIEW Q&A · 69 QUESTIONS

Object-Oriented Programming interview questions

The four pillars, interfaces, abstract classes and design basics.

  1. How do you do dependency injection in JavaScript without any framework?

    Pass the dependency as an argument, either to a factory function or a class constructor. A function that receives what it needs is already using dependency injection.

    BeginnerQuick answer1 min read
  2. How does dependency injection make unit testing easier?

    Because the class receives its collaborators, a test can pass in a fake instead of a real database, network or clock. The test runs fast and gives the same result every time.

    BeginnerQuick answer1 min read
  3. What are constructor injection and setter injection, and which should you prefer?

    Constructor injection passes dependencies when the object is created, so it is never half-built. Setter injection sets them afterwards. Prefer the constructor for required dependencies and setters only for optional ones.

    BeginnerQuick answer1 min read
  4. Why is creating dependencies with new inside a class considered a problem?

    It hard-wires the class to one concrete implementation, hides what it depends on, and makes it impossible to replace in tests or in other environments without editing the class.

    BeginnerQuick answer1 min read
  5. What does an IoC container do, and when is it worth using?

    It creates objects, works out their dependencies and injects them for you, often also controlling their lifetime. It saves manual wiring in large applications, but is unnecessary in small ones.

    IntermediateQuick answer1 min read
  6. What is the difference between Dependency Inversion, Dependency Injection and Inversion of Control?

    DIP is a design rule: depend on abstractions. DI is a technique: pass dependencies in from outside. IoC is the wider idea that a framework or caller controls the flow instead of your code.

    IntermediateQuick answer1 min read
  7. What is the difference between a Service Locator and Dependency Injection?

    With DI, dependencies are handed to the class from outside. With a Service Locator, the class asks a global registry for them. The locator hides what the class needs and makes testing harder.

    AdvancedQuick answer1 min read