Bird
Raised Fist0

What is wrong with this code?

medium📝 Debug Q7 of Q15
Python - Classes and Object Lifecycle
What is wrong with this code?
class Test:
    def __del__(self):
        print('Deleted')

obj = Test()
obj = None
ASyntax error in class definition
B__del__ may not be called immediately
CCannot assign None to obj
DMissing __init__ method
Step-by-Step Solution
Solution:
  1. Step 1: Understand __del__ timing

    Setting obj to None removes reference, but __del__ may not run immediately due to garbage collection timing.
  2. Step 2: Identify issue with __del__ call

    Python's garbage collector decides when to call __del__, so output may be delayed or not shown immediately.
  3. Final Answer:

    __del__ may not be called immediately -> Option B
  4. Quick Check:

    __del__ timing uncertain = May delay call [OK]
Quick Trick: __del__ runs when garbage collector frees object, timing varies [OK]
Common Mistakes:
MISTAKES
  • Expecting immediate __del__ call
  • Thinking assigning None deletes object instantly
  • Confusing __del__ with __init__

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes