Bird
Raised Fist0
Pythonprogramming~20 mins

Object lifecycle overview in Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Object Lifecycle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code involving object creation and deletion?

Consider the following Python code that creates and deletes an object. What will be printed?

Python
class MyClass:
    def __init__(self):
        print('Object created')
    def __del__(self):
        print('Object deleted')

obj = MyClass()
del obj
print('End of program')
A
Object created
End of program
Object deleted
B
Object created
Object deleted
End of program
C
End of program
Object created
Object deleted
D
Object deleted
Object created
End of program
Attempts:
2 left
💡 Hint

Think about when __init__ and __del__ methods are called.

🧠 Conceptual
intermediate
1:30remaining
When is the __del__ method called in Python?

Choose the correct statement about when Python calls the __del__ method of an object.

AWhen the object is created
BWhen the object is about to be destroyed by garbage collection
CWhen the program starts
DWhen the object is assigned to a new variable
Attempts:
2 left
💡 Hint

Think about object lifetime and memory cleanup.

Predict Output
advanced
2:30remaining
What is the output of this code with multiple references and deletion?

Analyze the code below. What will be printed?

Python
class Demo:
    def __init__(self):
        print('Created')
    def __del__(self):
        print('Deleted')

obj1 = Demo()
obj2 = obj1

del obj1
print('Deleted obj1')
del obj2
print('Deleted obj2')
A
2jbo deteleD
deteleD
1jbo deteleD
detaerC
B
Created
Deleted obj1
Deleted obj2
C
Created
Deleted obj1
Deleted
Deleted obj2
D
Created
Deleted obj1
Deleted obj2
Deleted
Attempts:
2 left
💡 Hint

Remember that the object is deleted only when all references are gone.

🔧 Debug
advanced
2:00remaining
Why does this __del__ method not always run?

Look at this code snippet. Sometimes 'Deleted' is not printed. Why?

Python
class Test:
    def __del__(self):
        print('Deleted')

def create():
    obj = Test()

create()
print('Done')
ABecause the program ends before garbage collection runs
BBecause __del__ is not a valid method name
CBecause the object is still referenced and not deleted immediately
DBecause the object is never created
Attempts:
2 left
💡 Hint

Think about when Python runs garbage collection and program exit behavior.

🧠 Conceptual
expert
2:00remaining
What happens if an exception is raised inside __del__?

Choose the correct statement about exceptions raised inside the __del__ method.

AThe exception is caught and logged by Python silently
BThe exception is propagated and stops the program immediately
CThe exception causes the object to never be deleted
DThe exception is ignored and a warning is printed to stderr
Attempts:
2 left
💡 Hint

Think about how Python handles errors during object destruction.

Practice

(1/5)
1. What is the first step in the lifecycle of a Python object?
easy
A. Creation of the object in memory
B. Deletion of the object
C. Garbage collection
D. Assignment of a variable

Solution

  1. Step 1: Understand object lifecycle start

    The lifecycle of a Python object begins when it is created in memory.
  2. Step 2: Differentiate from other lifecycle stages

    Deletion and garbage collection happen later, after creation and use.
  3. Final Answer:

    Creation of the object in memory -> Option A
  4. Quick Check:

    Object lifecycle starts with creation [OK]
Hint: Object lifecycle always starts with creation [OK]
Common Mistakes:
  • Confusing creation with deletion
  • Thinking garbage collection happens first
  • Assuming variable assignment is the first step
2. Which of the following is the correct syntax to define a destructor method in a Python class?
easy
A. def destructor(self):
B. def __del__(self):
C. def delete(self):
D. def __destroy__(self):

Solution

  1. Step 1: Recall destructor method name

    In Python, the destructor method is named __del__ with double underscores before and after.
  2. Step 2: Check syntax correctness

    The correct syntax is def __del__(self): which matches Python's special method naming.
  3. Final Answer:

    def __del__(self): -> Option B
  4. Quick Check:

    Destructor method = __del__ [OK]
Hint: Destructor method is always named __del__ [OK]
Common Mistakes:
  • Using wrong method names like destructor or delete
  • Missing double underscores
  • Confusing with constructor __init__
3. What will be the output of the following code?
class MyClass:
    def __init__(self):
        print('Created')
    def __del__(self):
        print('Deleted')

obj = MyClass()
print('Object in use')
del obj
print('End')
medium
A. Created\nObject in use\nDeleted\nEnd
B. Created\nDeleted\nObject in use\nEnd
C. Object in use\nCreated\nDeleted\nEnd
D. Created\nObject in use\nEnd

Solution

  1. Step 1: Trace object creation and constructor call

    When obj = MyClass() runs, __init__ prints 'Created'.
  2. Step 2: Follow print and deletion order

    Next, 'Object in use' prints. Then del obj calls __del__, printing 'Deleted'. Finally, 'End' prints.
  3. Final Answer:

    Created\nObject in use\nDeleted\nEnd -> Option A
  4. Quick Check:

    Constructor then prints, then destructor after del [OK]
Hint: Destructor runs only after del or object goes out of scope [OK]
Common Mistakes:
  • Assuming destructor runs immediately after creation
  • Ignoring order of print statements
  • Thinking del obj skips destructor
4. Identify the error in this code related to object lifecycle:
class Sample:
    def __init__(self):
        print('Init called')
    def __del__(self):
        print('Del called')

obj = Sample()
obj = None
print('Done')
medium
A. Destructor __del__ will not be called because obj is set to None
B. Syntax error in class definition
C. No error; destructor will be called when obj is set to None
D. Constructor __init__ will not be called

Solution

  1. Step 1: Understand what happens when obj is set to None

    Setting obj = None removes the reference to the Sample object, so it becomes eligible for garbage collection.
  2. Step 2: Confirm destructor call behavior

    When no references remain, __del__ is called, so 'Del called' will print before 'Done'.
  3. Final Answer:

    No error; destructor will be called when obj is set to None -> Option C
  4. Quick Check:

    Destructor runs when object has no references [OK]
Hint: Destructor runs when last reference is removed [OK]
Common Mistakes:
  • Thinking setting variable to None skips destructor
  • Confusing syntax errors with lifecycle behavior
  • Assuming constructor is skipped
5. Given a list of objects created inside a function, when will their destructors be called?
class Item:
    def __init__(self, name):
        self.name = name
        print(f'Created {name}')
    def __del__(self):
        print(f'Deleted {self.name}')

def create_items():
    items = [Item('a'), Item('b'), Item('c')]
    print('Items created')

create_items()
print('Function ended')
hard
A. Destructors called immediately after 'Items created' inside the function
B. Destructors called before 'Items created'
C. Destructors never called because list holds references
D. Destructors called after 'Function ended' when function scope ends

Solution

  1. Step 1: Analyze object references inside function

    Objects are stored in the list 'items' inside create_items(). They exist until the function ends.
  2. Step 2: Determine when objects lose references

    When create_items() finishes, 'items' list is destroyed, removing references to objects, triggering destructors.
  3. Step 3: Confirm output order

    So, 'Items created' prints, then function ends, then destructors print, then 'Function ended' prints.
  4. Final Answer:

    Destructors called after 'Function ended' when function scope ends -> Option D
  5. Quick Check:

    Objects destroyed after function scope ends [OK]
Hint: Objects destroyed when last reference goes out of scope [OK]
Common Mistakes:
  • Thinking destructors run immediately after creation
  • Assuming list keeps objects alive forever
  • Confusing print order with destructor timing