Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Python - Classes and Object Lifecycle
Consider this code:
class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

head = Node(1)
head.next = Node(2)
head = None

What happens to the first Node object after head = None?
AIt causes a runtime error
BIt remains accessible through head
CIt is immediately deleted and __del__ called
DIt becomes unreachable and eligible for garbage collection
Step-by-Step Solution
Solution:
  1. Step 1: Analyze references to first Node

    Setting head to None removes the reference to the first Node object.
  2. Step 2: Determine object reachability

    Since no other references point to the first Node, it becomes unreachable and can be garbage collected.
  3. Final Answer:

    It becomes unreachable and eligible for garbage collection -> Option D
  4. Quick Check:

    Unreferenced objects = Eligible for garbage collection [OK]
Quick Trick: Objects without references are cleaned up by garbage collector [OK]
Common Mistakes:
  • Thinking object remains accessible
  • Expecting immediate deletion
  • Assuming runtime error occurs

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes