Hack4Hack

OOPs practice questions with answers

Practise OOPs questions at your own pace: encapsulation, inheritance, polymorphism, SOLID and design patterns. Pick an answer to see whether you are right and why. Nothing is timed or ranked here; for that, play the Daily Quiz at 4 PM IST.

  1. What are the four pillars of object-oriented programming?

    easy

    1. Encapsulation, abstraction, inheritance, polymorphism
    2. Classes, objects, methods, fields
    3. Compilation, linking, loading, execution
    4. Variables, loops, functions, modules

    Answer: A. Encapsulation, abstraction, inheritance, polymorphism

    Explanation: Encapsulation hides internal state, abstraction exposes only what matters, inheritance reuses behaviour, polymorphism lets one interface have many forms.

  2. What is encapsulation?

    easy

    1. Inheriting from many classes
    2. Keeping data and the methods that use it together, and hiding internal details behind an interface
    3. Overloading operators
    4. Writing code in capsules

    Answer: B. Keeping data and the methods that use it together, and hiding internal details behind an interface

    Explanation: Private fields with public methods let a class protect its invariants and change its internals without breaking callers.

  3. Method overloading is:

    easy

    1. A subclass redefining a parent's method
    2. Several methods with the same name but different parameters in one class
    3. Calling a method too often
    4. A runtime error

    Answer: B. Several methods with the same name but different parameters in one class

    Explanation: Overloading is resolved at compile time. Overriding (a subclass redefining a method) is resolved at run time.

  4. What is a constructor?

    easy

    1. A method that destroys objects
    2. A special method that initialises a new object
    3. A static variable
    4. A type of loop

    Answer: B. A special method that initialises a new object

    Explanation: It runs when an object is created and sets up its initial state.

  5. What does "favour composition over inheritance" mean?

    medium

    1. Never use inheritance
    2. Build behaviour by holding other objects (has-a) rather than deep class hierarchies (is-a), for looser coupling
    3. Use only interfaces
    4. Put all code in one class

    Answer: B. Build behaviour by holding other objects (has-a) rather than deep class hierarchies (is-a), for looser coupling

    Explanation: Composition lets you swap parts at run time and avoids fragile base classes.

  6. Which SOLID principle says a class should have only one reason to change?

    medium

    1. Open/closed
    2. Single responsibility
    3. Liskov substitution
    4. Dependency inversion

    Answer: B. Single responsibility

    Explanation: SRP: one responsibility per class. Open/closed: extend without modifying. Liskov: subtypes must be usable wherever the base type is.

  7. A Square subclass of Rectangle breaks code that sets width and height independently. Which principle is violated?

    medium

    1. Single responsibility
    2. Liskov substitution
    3. Interface segregation
    4. DRY

    Answer: B. Liskov substitution

    Explanation: Code written for Rectangle expects width and height to change separately; Square cannot honour that, so it is not a true substitute.

  8. What is the diamond problem?

    medium

    1. A memory leak
    2. Ambiguity when a class inherits the same member from two parents that share a base class
    3. A sorting bug
    4. Recursive constructors

    Answer: B. Ambiguity when a class inherits the same member from two parents that share a base class

    Explanation: C++ solves it with virtual inheritance, Python with its method resolution order, and Java avoids it by allowing only one superclass.

  9. Which design pattern makes sure a class has exactly one instance?

    hard

    1. Factory
    2. Singleton
    3. Observer
    4. Strategy

    Answer: B. Singleton

    Explanation: Singletons are easy to misuse: they are global state and make testing harder. Dependency injection is often a better choice.

  10. Which pattern notifies a list of subscribers when an object's state changes?

    hard

    1. Adapter
    2. Observer
    3. Decorator
    4. Builder

    Answer: B. Observer

    Explanation: Event listeners, pub/sub systems and reactive UIs are all built on the observer idea.

  11. What is dependency inversion?

    hard

    1. Subclasses depend on parents
    2. High-level code depends on abstractions (interfaces), not concrete classes, so implementations can be swapped
    3. Removing all dependencies
    4. Calling methods in reverse order

    Answer: B. High-level code depends on abstractions (interfaces), not concrete classes, so implementations can be swapped

    Explanation: For example, a service takes a PaymentGateway interface, so tests can pass a fake and production a real gateway.

  12. Which are examples of runtime polymorphism? (Select all that apply)

    hard

    1. Overriding a method in a subclass and calling it through a base reference
    2. Implementing an interface method in several classes
    3. Overloading a method with different parameter types
    4. Calling a virtual function in C++ through a pointer

    Answer: A. Overriding a method in a subclass and calling it through a base reference; B. Implementing an interface method in several classes; D. Calling a virtual function in C++ through a pointer

    Explanation: Overloading is decided at compile time (static polymorphism). The others choose the implementation from the actual object at run time.

Play the Daily Quiz at 4 PM IST