0
0
Pythonprogramming~20 mins

Object lifecycle overview in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
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.