Bird
Raised Fist0

What will be the output of the following code?

medium📝 Predict Output Q13 of Q15
Python - Classes and Object Lifecycle
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')
ACreated\nObject in use\nDeleted\nEnd
BCreated\nDeleted\nObject in use\nEnd
CObject in use\nCreated\nDeleted\nEnd
DCreated\nObject in use\nEnd
Step-by-Step Solution
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]
Quick Trick: Destructor runs only after del or object goes out of scope [OK]
Common Mistakes:
MISTAKES
  • Assuming destructor runs immediately after creation
  • Ignoring order of print statements
  • Thinking del obj skips destructor

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes