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. Explain the Open/Closed Principle with a payment example.

    Classes should be open for extension but closed for modification. To add a new payment method, write a new class that implements an interface instead of editing the checkout code.

    BeginnerQuick answer1 min read
  2. Does the Open/Closed Principle mean you can never edit an existing class?

    No. You still fix bugs and change requirements. OCP is about designing so that common kinds of change, such as adding a new variant, need new code instead of edits to old code.

    IntermediateQuick answer1 min read
  3. Show the Open/Closed Principle in JavaScript with a list of validation rules.

    Keep rules as a list of small functions and run them all with one loop. A new rule is added by appending a function, and the validating code is never changed.

    IntermediateQuick answer1 min read