Object Oriented Programming Interview Questions
Comprehensive object oriented programming interview questions and answers for Python. Prepare for your next job interview with expert guidance.
Questions Overview
1. What are classes and objects in Python? How do they differ?
Basic2. Explain inheritance in Python and how to implement it.
Basic3. What are class methods, static methods, and instance methods? How do they differ?
Moderate4. How does Python implement encapsulation?
Moderate5. What are magic methods (dunder methods) and how are they used?
Moderate6. How do you implement polymorphism in Python?
Moderate7. What are properties in Python and when should they be used?
Advanced8. Explain multiple inheritance and method resolution order (MRO) in Python.
Advanced9. What are metaclasses and when would you use them?
Advanced10. How do descriptors work in Python?
Advanced11. What is composition and how does it compare to inheritance?
Moderate12. How do you implement abstract classes in Python?
Advanced13. What are class and instance variables? How do they differ?
Basic14. How does super() work in Python?
Moderate15. What is the difference between __new__ and __init__?
Advanced16. How do you implement a singleton pattern in Python?
Advanced17. What are slots and when should they be used?
Advanced18. How do you handle attribute access in Python classes?
Advanced19. What is method overriding and when should it be used?
Basic20. How do you implement operator overloading in Python?
Moderate21. What are class decorators and how are they used?
Advanced22. How do you implement immutable classes in Python?
Advanced23. What is the role of __repr__ vs __str__?
Moderate24. How do you implement context managers using classes?
Moderate25. What are mixins and when should they be used?
Advanced26. How do you handle object serialization in Python?
Moderate27. What are dataclasses and when should they be used?
Moderate28. How do you implement custom container classes?
Advanced29. What is the role of __dict__ in Python classes?
Moderate1. What are classes and objects in Python? How do they differ?
BasicA class is a blueprint for objects, defining attributes and methods. An object is an instance of a class. Classes define structure and behavior, while objects contain actual data. Example: class Dog defines properties like breed, while a specific dog object represents an actual dog with those properties.
2. Explain inheritance in Python and how to implement it.
BasicInheritance allows a class to inherit attributes and methods from another class. Implemented using class Child(Parent). Supports single, multiple, and multilevel inheritance. Example: class Car(Vehicle) inherits from Vehicle class. Use super() to access parent class methods.
3. What are class methods, static methods, and instance methods? How do they differ?
ModerateInstance methods (default) have self parameter, access instance data. Class methods (@classmethod) have cls parameter, access class data. Static methods (@staticmethod) have no special first parameter, don't access class/instance data. Each serves different purpose in class design.
4. How does Python implement encapsulation?
ModeratePython uses name mangling with double underscore prefix (__var) for private attributes. Single underscore (_var) for protected attributes (convention). No true private variables, but follows 'we're all consenting adults' philosophy. Access control through properties and descriptors.
5. What are magic methods (dunder methods) and how are they used?
ModerateMagic methods control object behavior for built-in operations. Examples: __init__ for initialization, __str__ for string representation, __len__ for length, __getitem__ for indexing. Enable operator overloading and customize object behavior.
6. How do you implement polymorphism in Python?
ModeratePolymorphism implemented through method overriding in inheritance and duck typing. Same method name behaves differently for different classes. No need for explicit interface declarations. Example: different classes implementing same method name but different behaviors.
7. What are properties in Python and when should they be used?
AdvancedProperties (@property decorator) provide getter/setter functionality with attribute-like syntax. Control access to attributes, add validation, make attributes read-only. Example: @property for getter, @name.setter for setter. Used for computed attributes and encapsulation.
8. Explain multiple inheritance and method resolution order (MRO) in Python.
AdvancedMultiple inheritance allows class to inherit from multiple parents: class Child(Parent1, Parent2). MRO determines method lookup order using C3 linearization algorithm. Access MRO using Class.__mro__. Handle diamond problem through proper method resolution.
9. What are metaclasses and when would you use them?
AdvancedMetaclasses are classes for classes, allow customizing class creation. Created using type or custom metaclass. Used for API creation, attribute/method validation, class registration, abstract base classes. Example: ABCMeta for abstract classes.
10. How do descriptors work in Python?
AdvancedDescriptors control attribute access through __get__, __set__, __delete__ methods. Used in properties, methods, class attributes. Enable reusable attribute behavior. Example: implementing validation, computed attributes, or attribute access logging.
11. What is composition and how does it compare to inheritance?
ModerateComposition creates objects containing other objects as parts (has-a relationship), while inheritance creates is-a relationships. Composition more flexible, reduces coupling. Example: Car has-a Engine vs. ElectricCar is-a Car.
12. How do you implement abstract classes in Python?
AdvancedUse abc module with ABC class and @abstractmethod decorator. Abstract classes can't be instantiated, enforce interface implementation. Example: from abc import ABC, abstractmethod. Used for defining common interfaces and ensuring implementation.
13. What are class and instance variables? How do they differ?
BasicClass variables shared among all instances (defined in class), instance variables unique to each instance (defined in __init__). Class variables accessed through class or instance, modified through class. Be careful with mutable class variables.
14. How does super() work in Python?
Moderatesuper() returns proxy object for delegating method calls to parent class. Handles multiple inheritance correctly using MRO. Used in __init__ and other overridden methods. Example: super().__init__() calls parent's __init__.
15. What is the difference between __new__ and __init__?
Advanced__new__ creates instance, called before __init__. __init__ initializes instance after creation. __new__ rarely overridden except for singletons, immutables. __new__ is static method, __init__ is instance method.
16. How do you implement a singleton pattern in Python?
AdvancedImplement using metaclass, __new__ method, or module-level instance. Control instance creation, ensure single instance exists. Handle thread safety if needed. Example: override __new__ to return existing instance or decorator approach.
17. What are slots and when should they be used?
Advanced__slots__ restricts instance attributes to fixed set, reduces memory usage. Faster attribute access, prevents dynamic attribute addition. Trade flexibility for performance. Used in classes with fixed attribute set.
18. How do you handle attribute access in Python classes?
AdvancedUse __getattr__, __setattr__, __getattribute__, __delattr__ for custom attribute access. __getattr__ called for missing attributes, __getattribute__ for all attribute access. Be careful with infinite recursion.
19. What is method overriding and when should it be used?
BasicMethod overriding redefines method from parent class in child class. Used to specialize behavior while maintaining interface. Call parent method using super() when needed. Example: overriding __str__ for custom string representation.
20. How do you implement operator overloading in Python?
ModerateOverride special methods for operators: __add__ for +, __eq__ for ==, etc. Enable class instances to work with built-in operators. Example: implement __lt__ for sorting. Consider reverse operations (__radd__, etc.).
21. What are class decorators and how are they used?
AdvancedClass decorators modify or enhance class definitions. Applied using @decorator syntax above class. Can add attributes, methods, or modify class behavior. Example: dataclass decorator for automatic __init__, __repr__.
22. How do you implement immutable classes in Python?
AdvancedUse __slots__, @property with only getter, override __setattr__/__delattr__. Store data in private tuples, implement __new__ instead of __init__. Consider frozen dataclasses. Make all instance data immutable.
23. What is the role of __repr__ vs __str__?
Moderate__str__ for human-readable string representation, __repr__ for unambiguous representation (debugging). __repr__ should be complete enough to recreate object if possible. Default to __repr__ if __str__ not defined.
24. How do you implement context managers using classes?
ModerateImplement __enter__ and __exit__ methods. __enter__ sets up context, __exit__ handles cleanup. Used with 'with' statement. Alternative: @contextmanager decorator for function-based approach.
25. What are mixins and when should they be used?
AdvancedMixins are classes providing additional functionality through multiple inheritance. Used for reusable features across different classes. Keep mixins focused, avoid state. Example: LoggerMixin for adding logging capability.
26. How do you handle object serialization in Python?
ModerateUse pickle for Python-specific serialization, implement __getstate__/__setstate__ for custom serialization. Consider JSON serialization with custom encoders/decoders. Handle security implications of deserialization.
27. What are dataclasses and when should they be used?
ModerateDataclasses (@dataclass) automatically add generated methods (__init__, __repr__, etc.). Used for classes primarily storing data. Support comparison, frozen instances, inheritance. More concise than manual implementation.
28. How do you implement custom container classes?
AdvancedImplement container protocol methods: __len__, __getitem__, __setitem__, __delitem__, __iter__. Optional: __contains__, __reversed__. Consider collections.abc base classes. Example: custom list or dictionary implementation.
29. What is the role of __dict__ in Python classes?
Moderate__dict__ stores instance attributes in dictionary. Enables dynamic attribute addition. Not present when using __slots__. Accessed for introspection, serialization. Consider memory implications for many instances.