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. Does the Single Responsibility Principle mean a class should have only one method?

    No. It means a class should have one reason to change, serving one actor or concern. A class can have many methods if they all serve that single purpose.

    BeginnerQuick answer1 min read
  3. How do you spot a Single Responsibility violation in real code?

    Look for a class whose description needs the word and, imports from unrelated areas, unrelated groups of fields, or edits from different people for different reasons.

    IntermediateQuick answer1 min read
  4. How is the Interface Segregation Principle different from the Single Responsibility Principle?

    SRP is about a class having one reason to change. ISP is about the interface a client sees: it should be small and fit that client's needs. One is about implementation, the other about the contract.

    IntermediateQuick answer1 min read