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 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
  2. Show Dependency Inversion with a NotificationService that currently creates EmailSender inside itself.

    Define a Sender interface, have NotificationService depend on it, and pass the implementation in from outside. The service no longer knows whether it is sending email, SMS or WhatsApp.

    BeginnerQuick answer1 min read
  3. What is duck typing in Python, and how does it relate to polymorphism?

    Duck typing means an object is accepted if it has the methods you call, regardless of its class. It gives polymorphism without a shared parent or interface.

    BeginnerQuick answer1 min read
  4. What is self in Python class methods, and why must it be written?

    self is the first parameter of an instance method and receives the object the method was called on. Python passes it automatically; the name is only a convention.

    BeginnerQuick answer1 min read