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. Explain the Interface Segregation Principle with a Worker interface example.

    No class should be forced to implement methods it does not use. Split one big interface into small, focused ones so each class picks only what applies.

    BeginnerQuick answer1 min read
  4. 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
  5. How do you achieve abstraction in Java?

    Use abstract classes (partial implementations with abstract methods) or interfaces (pure contracts). Callers depend on the abstract type and never see the concrete class behind it.

    BeginnerQuick answer1 min read
  6. 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
  7. 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
  8. Is encapsulation the same as data hiding?

    Not exactly. Encapsulation bundles data and the methods that work on it into one unit. Data hiding is the part where the fields are made private so outsiders can only use the safe methods.

    BeginnerQuick answer1 min read
  9. 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
  10. 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
  11. What are the types of inheritance, and which does Java support through classes?

    Single, multilevel, hierarchical, multiple and hybrid. Java supports the first three with classes; multiple inheritance of classes is not allowed, but a class can implement many interfaces.

    BeginnerQuick answer1 min read
  12. What are the uses of the super keyword?

    super refers to the parent part of the current object. It calls the parent constructor, calls a parent method that the child has overridden, or reaches a parent field hidden by a child field.

    BeginnerQuick answer1 min read
  13. What does the this keyword mean in Java, and where do you use it?

    this is a reference to the current object, the one whose method or constructor is running. It is used to separate fields from parameters of the same name, to call another constructor, and to pass the current object along.

    BeginnerQuick answer1 min read
  14. 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
  15. 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
  16. What is the difference between == and equals() in Java?

    == compares references for objects (are they the same object?) and values for primitives. equals() compares content, but only if the class overrides it; the default version behaves like ==.

    BeginnerQuick answer1 min read
  17. What is the difference between a class and an object?

    A class is a blueprint that describes the fields and methods something will have. An object is one real instance built from that blueprint, with its own values stored in memory.

    BeginnerQuick answer1 min read
  18. What is the difference between abstraction and encapsulation?

    Abstraction decides what to show: it exposes a simple view and hides complexity. Encapsulation decides how to protect: it bundles data with methods and restricts access to it.

    BeginnerQuick answer1 min read
  19. What is the difference between instance and static members of a class?

    An instance member belongs to each object and has a separate copy per object. A static member belongs to the class itself, exists once, and is shared by every object.

    BeginnerQuick answer1 min read
  20. What is the Observer pattern? Explain with a cricket score example.

    A subject keeps a list of observers and tells all of them when its state changes. Observers subscribe and unsubscribe at runtime, and the subject does not need to know what they do with the news.

    BeginnerQuick answer1 min read
  21. What is the Singleton pattern and how do you implement it?

    Singleton ensures a class has exactly one instance and gives a global access point to it. Make the constructor private, keep the instance in a static field, and return it from a static method.

    BeginnerQuick answer1 min read
  22. When does Java provide a default constructor, and when does it not?

    If you write no constructor at all, the compiler adds a public no-argument one. The moment you write any constructor yourself, the compiler stops adding it.

    BeginnerQuick answer1 min read
  23. 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
  24. Why use a factory instead of calling new directly?

    A factory hides which concrete class is created and how. Callers ask for what they need, and creation rules can change in one place without touching the callers.

    BeginnerQuick answer1 min read
  25. Why use getters and setters when we could make fields public?

    Methods let a class validate input, compute values, log changes, and change its internal storage later without breaking callers. A public field gives up all of that control.

    BeginnerQuick answer1 min read
  26. Are constructors and private members inherited in Java?

    Constructors are never inherited; a child must define its own and call the parent's with super(). Private members exist inside the child object but cannot be accessed by name from the child.

    IntermediateQuick answer1 min read
  27. Can an abstract class have a constructor? Why would it need one?

    Yes. It cannot be instantiated directly, but a subclass constructor calls it with super() so the parent's fields get initialised.

    IntermediateQuick answer1 min read
  28. Can static methods be overridden in Java?

    No. A static method in a child with the same signature hides the parent's one. The call is resolved at compile time from the reference type, so there is no runtime polymorphism.

    IntermediateQuick answer1 min read
  29. Does Python have private attributes? What do _x and __x mean?

    Python has no true private. A single underscore _x is a hint that it is internal. A double underscore __x triggers name mangling, which renames it to _ClassName__x to avoid clashes in subclasses.

    IntermediateQuick answer1 min read
  30. 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