Object-Oriented Programming — Notes & 50 MCQs

Comprehensive OOP Notes

This guide covers core OOP concepts, practical examples, best practices, and design principles useful for interviews and exams.

Classes & Objects

Class: blueprint describing data (attributes) and behavior (methods). Object: instance of a class. Constructors initialize object state. Destructors cleanup (language-specific).

// Example (Java-like)\nclass Person {\n  String name; int age;\n  Person(String n, int a){ name=n; age=a; }\n}\n

Encapsulation

Encapsulation hides internal state and exposes behavior via methods. Use access modifiers (private, protected, public). Benefits: modularity, maintainability.

Abstraction

Abstraction exposes essential features and hides implementation details. Achieved via abstract classes and interfaces. Focuses on 'what' rather than 'how'.

Inheritance

Inheritance models 'is-a' relationships. Single, multiple (language-dependent), multilevel, hierarchical. Use super/parent classes for shared behavior. Beware tight coupling.

Polymorphism

Polymorphism allows entities to take multiple forms. Compile-time (method overloading, operator overloading) and runtime (method overriding via virtual methods).

Interfaces & Abstract Classes

Interfaces define contracts (method signatures) without implementation (some languages allow default methods). Abstract classes can include partial implementation and abstract methods.

Constructors & Destructors

Constructors initialize objects. Overloaded constructors provide multiple ways to create objects. Destructors or finalizers are used for resource release (use RAII in C++).

Method Overloading vs Overriding

Overloading: same method name, different parameters (compile-time). Overriding: subclass provides specific implementation (runtime polymorphism).

SOLID Principles (short)

  • S — Single Responsibility
  • O — Open/Closed
  • L — Liskov Substitution
  • I — Interface Segregation
  • D — Dependency Inversion

Design Patterns (common)

Factory, Singleton, Strategy, Observer, Decorator, Adapter — patterns solve recurring design problems.

Memory & Performance

Be aware of object creation cost, garbage collection pauses, prefer composition over inheritance when appropriate, avoid deep inheritance hierarchies.

50 Practice MCQs — Correct answers highlighted

1. Which OOP concept binds data and methods that operate on the data together?

  • A) Encapsulation ✅
  • B) Inheritance
  • C) Polymorphism
  • D) Abstraction

2. What is inheritance primarily used for?

  • A) Hiding implementation
  • B) Code reuse and establishing IS-A relationship ✅
  • C) Data encryption
  • D) Resource allocation

3. Which is an example of runtime polymorphism?

  • A) Method overloading
  • B) Method overriding ✅
  • C) Operator overloading
  • D) Generic programming

4. Which keyword is used to create a subclass in Java?

  • A) extends ✅
  • B) implements
  • C) inherits
  • D) super

5. Which principle states a class should have only one reason to change?

  • A) Single Responsibility Principle (SRP) ✅
  • B) Open-Closed Principle
  • C) Liskov Substitution Principle
  • D) Interface Segregation Principle

6. Which access modifier allows members to be visible only within the same class?

  • A) private ✅
  • B) public
  • C) protected
  • D) internal

7. What does 'this' keyword refer to in instance methods?

  • A) Current object instance ✅
  • B) Parent class
  • C) Static context
  • D) Class loader

8. Which concept hides complex implementation details and shows only necessary features?

  • A) Abstraction ✅
  • B) Encapsulation
  • C) Inheritance
  • D) Polymorphism

9. What is method overloading?

  • A) Same method name with different parameter lists ✅
  • B) Same signature, different return types
  • C) Overriding parent methods
  • D) Changing access modifier

10. Which OOP concept allows treating objects of different classes through a common interface?

  • A) Polymorphism ✅
  • B) Inheritance
  • C) Encapsulation
  • D) Abstraction

11. Which pattern ensures a class has only one instance?

  • A) Singleton ✅
  • B) Factory
  • C) Observer
  • D) Strategy

12. Which concept implies 'has-a' relationship?

  • A) Composition/Aggregation ✅
  • B) Inheritance
  • C) Polymorphism
  • D) Encapsulation

13. What is the correct term for functions defined inside a class?

  • A) Methods ✅
  • B) Procedures
  • C) Routines
  • D) Lambdas

14. Which of these is not a benefit of OOP?

  • A) Modularity
  • B) Reusability
  • C) Faster execution inherently ✅
  • D) Maintainability

15. Which keyword is used to call parent class constructor in many languages?

  • A) super ✅
  • B) base
  • C) parent
  • D) this

16. What is multiple inheritance?

  • A) A class inheriting from more than one parent class ✅
  • B) A class with multiple constructors
  • C) Method overloading
  • D) Implementing multiple interfaces only

17. Which language feature avoids multiple inheritance problems by using interfaces?

  • A) Interfaces (Java) ✅
  • B) Abstract classes
  • C) Mixins
  • D) Traits

18. Which OOP principle allows changing implementation without affecting users of the abstraction?

  • A) Abstraction ✅
  • B) Inheritance
  • C) Polymorphism
  • D) Encapsulation

19. What is Liskov Substitution Principle about?

  • A) Subtypes must be substitutable for their base types ✅
  • B) Classes should be open for modification
  • C) Interfaces should be broad
  • D) Use singletons sparingly

20. Which pattern defines a family of algorithms and makes them interchangeable?

  • A) Strategy Pattern ✅
  • B) Singleton Pattern
  • C) Observer Pattern
  • D) Adapter Pattern

21. In which situation is composition preferred over inheritance?

  • A) When you need to reuse behavior without creating tight coupling ✅
  • B) When classes share an IS-A relationship
  • C) When you need static methods only
  • D) For primitive data only

22. Which OOP principle restricts direct access to some of an object's components?

  • A) Encapsulation ✅
  • B) Polymorphism
  • C) Inheritance
  • D) Abstraction

23. Which is true about abstract classes?

  • A) They can have both abstract and concrete methods ✅
  • B) They cannot have any implemented methods
  • C) They can be instantiated directly
  • D) They replace interfaces entirely

24. What does 'interface segregation' encourage?

  • A) Splitting large interfaces into smaller, client-specific ones ✅
  • B) Merging interfaces for reuse
  • C) Using singletons for all services
  • D) Avoiding interfaces

25. Which of the following best describes method overriding?

  • A) Subclass provides specific implementation of a parent method ✅
  • B) Same method name different parameters
  • C) Private methods in base class
  • D) Static binding only

26. What is coupling in software design?

  • A) Degree of interdependence between modules ✅
  • B) How modules are cohesive internally
  • C) Number of classes in a package
  • D) Depth of inheritance tree only

27. Which pattern allows an object to notify other objects about changes?

  • A) Observer Pattern ✅
  • B) Factory Pattern
  • C) Singleton Pattern
  • D) Adapter Pattern

28. Which access modifier allows visibility to subclasses and same package (in Java)?

  • A) protected ✅
  • B) public
  • C) private
  • D) package-private only

29. What is a constructor?

  • A) Special method to initialize new objects ✅
  • B) Method to destruct objects
  • C) Static factory only
  • D) Method with return type

30. What does 'open/closed' principle state?

  • A) Software entities should be open for extension, closed for modification ✅
  • B) Open source over closed source
  • C) Classes should be publicly visible
  • D) Methods should be mutable

31. Which term refers to grouping related variables and functions together?

  • A) Encapsulation ✅
  • B) Inheritance
  • C) Polymorphism
  • D) Abstraction

32. Which pattern provides a surrogate or placeholder for another object?

  • A) Proxy Pattern ✅
  • B) Builder Pattern
  • C) Strategy Pattern
  • D) Observer Pattern

33. Which is true about static members?

  • A) Shared across all instances of a class ✅
  • B) Unique per instance
  • C) Cannot be accessed without object
  • D) Are instance initialization blocks

34. What is method signature?

  • A) Method name and parameter types (and order) ✅
  • B) Return type only
  • C) Access modifier only
  • D) Exception list only

35. Which SOLID principle promotes depending on abstractions rather than concretions?

  • A) Dependency Inversion Principle ✅
  • B) Single Responsibility Principle
  • C) Interface Segregation Principle
  • D) Liskov Substitution Principle

36. Which pattern creates objects without exposing the instantiation logic?

  • A) Factory Pattern ✅
  • B) Prototype Pattern
  • C) Decorator Pattern
  • D) Composite Pattern

37. What is the effect of declaring a method final (Java)?

  • A) Method cannot be overridden in subclasses ✅
  • B) Method cannot be called
  • C) Method becomes static
  • D) Method is abstract

38. Which principle suggests small, specific interfaces over large ones?

  • A) Interface Segregation Principle ✅
  • B) Open/Closed
  • C) Dependency Inversion
  • D) Single Responsibility

39. Which term describes hiding internal state and requiring all interaction through methods?

  • A) Encapsulation ✅
  • B) Polymorphism
  • C) Inheritance
  • D) Composition

40. Which is true about interfaces?

  • A) They define method signatures without implementation (language-dependent exceptions) ✅
  • B) They can be instantiated directly
  • C) They store state like classes
  • D) They are the same as abstract classes

41. What is a mixin?

  • A) Class containing methods for reuse that can be combined with other classes ✅
  • B) A final class
  • C) A singleton instance
  • D) A type of interface only

42. Which concept helps avoid code duplication by extracting common behavior?

  • A) Abstraction and inheritance ✅
  • B) Encapsulation only
  • C) Polymorphism only
  • D) Final classes

43. Which design pattern adds behavior to objects dynamically?

  • A) Decorator Pattern ✅
  • B) Factory Pattern
  • C) Singleton Pattern
  • D) Strategy Pattern

44. What is method hiding (static methods) difference from overriding?

  • A) Hiding occurs with static methods and is resolved at compile-time ✅
  • B) Hiding is same as overriding
  • C) Hiding happens with private methods only
  • D) Hiding allows polymorphism

45. Which pattern provides a way to access elements of an aggregate object sequentially?

  • A) Iterator Pattern ✅
  • B) Composite Pattern
  • C) Adapter Pattern
  • D) Observer Pattern

46. What is delegation?

  • A) An object handles a request by passing it to a helper object ✅
  • B) Inheriting behavior directly
  • C) Static binding
  • D) Method overriding only

47. Which pattern composes objects into tree structures to represent part-whole hierarchies?

  • A) Composite Pattern ✅
  • B) Builder Pattern
  • C) Prototype Pattern
  • D) Flyweight Pattern

48. What is a shallow copy?

  • A) Copying object's fields but not the objects they reference (references shared) ✅
  • B) Deeply cloning all nested objects
  • C) Copying only static fields
  • D) Copying via serialization only

49. Which term describes the number of subclasses a class has?

  • A) Class fan-out (or breadth) ✅
  • B) Inheritance depth only
  • C) Coupling factor
  • D) Cohesion

50. Which practice reduces tight coupling and increases testability?

  • A) Dependency Injection ✅
  • B) Using singletons everywhere
  • C) Extensive static methods
  • D) Deep inheritance chains
"""