Intermediate 50 Questions • ~10 hrs study

Python OOP (Q51-100)

Object-Oriented Programming in Python — classes, inheritance, polymorphism, encapsulation, abstract classes, and design patterns.

51

What are the four pillars of OOP?

Short Answer

Encapsulation (bundling data + methods), Abstraction (hiding complexity), Inheritance (reusing code from parent), Polymorphism (same interface, different behavior).

Key Points

  • Encapsulation: Use private attributes (_name, __name)
  • Abstraction: ABC module, abstract methods
  • Inheritance: single, multiple, multilevel
  • Polymorphism: method overriding, duck typing

💡 Memory Trick: "A PIE — Abstraction, Polymorphism, Inheritance, Encapsulation"

52

What is the difference between __init__ and __new__?

Short Answer

__new__ creates the instance (allocates memory), __init__ initializes it (sets attributes). __new__ is called first.

class Singleton:
    _instance = None
    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance
    
    def __init__(self):
        self.value = 42

💡 Memory Trick: "__new__ = Builder (creates house) | __init__ = Interior Designer (furnishes it)"

53

Explain Method Resolution Order (MRO) in Python.

Short Answer

MRO determines the order in which base classes are searched when calling a method. Python uses the C3 Linearization algorithm. Check with ClassName.__mro__ or ClassName.mro().

class A: pass
class B(A): pass
class C(A): pass
class D(B, C): pass

print(D.__mro__)
# (D, B, C, A, object)

💡 Memory Trick: "MRO = Left to Right, Depth Last (C3 linearization)"

54

What is the difference between @staticmethod and @classmethod?

Short Answer

@staticmethod doesn't receive cls or self — it's a regular function in a class namespace. @classmethod receives the class (cls) as first argument and can access/modify class state.

class Model:
    version = "1.0"
    
    @classmethod
    def get_version(cls):
        return cls.version  # Access class attribute
    
    @staticmethod
    def validate_input(x):
        return x > 0  # No access to class/instance

💡 Memory Trick: "classmethod = knows its CLASS | staticmethod = STANDALONE utility"

Questions 55-100 continue with the same detailed format.

Topics covered: Abstract Base Classes, Property Decorators, Dunder Methods (__str__, __repr__, __eq__), Descriptors, Slots, Mixins, Multiple Inheritance Diamond Problem, Composition vs Inheritance, Design Patterns (Singleton, Factory, Observer), Dataclasses, and more.

View Full Documentation →