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. A class calculates salary, saves it to the database and prints a payslip. How does it break SRP, and how do you fix it?

    It has three reasons to change: pay rules, database layout and payslip format. Split it into three classes, each with one job, and let a small coordinator call them.

    BeginnerQuick answer1 min read
  2. How can polymorphism replace a long if-else or switch chain?

    Give each case its own class with the same method, then call that method on the object. Adding a new case means adding a class, not editing the old, tested chain.

    IntermediateQuick answer1 min read
  3. Your interface has twelve methods and most implementers leave several empty. How do you fix it without breaking everything?

    Introduce smaller role interfaces, make the big interface extend them, move clients to the small ones gradually, then retire the old one. Existing code keeps compiling throughout.

    AdvancedQuick answer1 min read