Logo Dark

Python Most Common Interview Questions

Python Most Common Interview Questions

Table of Content

    1. How does Python manage memory? Explain the role of reference counting and garbage collection.

    Python uses reference counting as its primary memory management technique, where objects are deallocated as soon as their reference count reaches zero. However, since reference counting cannot handle circular references, Python also uses a cyclic garbage collector that identifies unreachable objects and cleans them periodically.”

    import sys
    sys.getrefcount(obj)
    import gc
    gc.collect()

    2. What is the difference between a static method, a class method, and an instance method?

    Instance methods operate on object data using self, class methods operate on class-level data using cls, and static methods are utility functions that don’t depend on either class or instance.

    ❓ Can a static method access class variables?

    👉 Yes, but only by explicitly using the class name

    🔹 When to Use What?

    Instance Method

    👉 When working with object data
    Example: user.get_name()

    Class Method

    👉 When working with class-level logic
    Example: factory methods, counters

    Static Method

    👉 When logic is independent
    Example: utility/helper functions