Challenge - 5 Problems
Self Reference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a self-referencing dictionary
What is the output of this Python code that uses a dictionary referencing itself?
Python
d = {}
d['self'] = d
print(d['self'] is d)Attempts:
2 left
💡 Hint
Think about whether the dictionary stored inside the key 'self' is the same object as d.
✗ Incorrect
The dictionary d stores itself under the key 'self'. So d['self'] points to the same dictionary object as d, making the identity check True.
❓ Predict Output
intermediate2:00remaining
Output of a self-referencing list
What will this Python code print?
Python
lst = [1, 2] lst.append(lst) print(len(lst))
Attempts:
2 left
💡 Hint
Appending the list to itself adds one more element.
✗ Incorrect
The list initially has 2 elements. Appending itself adds a third element which is the list itself, so length becomes 3.
🔧 Debug
advanced3:00remaining
Why does this self-referencing class cause infinite recursion?
Consider this Python class that references itself in a method. What causes the infinite recursion error?
Python
class Node: def __init__(self): self.child = self def get_child(self): return self.child.get_child() n = Node() n.get_child()
Attempts:
2 left
💡 Hint
Look at what get_child returns and what self.child points to.
✗ Incorrect
The method get_child calls itself recursively because self.child is the same Node instance, so get_child calls get_child endlessly causing infinite recursion.
🧠 Conceptual
advanced2:00remaining
Understanding self-reference in nested data structures
What is the output of this code that creates a nested dictionary referencing itself?
Python
a = {}
a['nested'] = {'parent': a}
print(a['nested']['parent'] is a)Attempts:
2 left
💡 Hint
Check if the nested dictionary's 'parent' key points back to the original dictionary.
✗ Incorrect
The nested dictionary inside 'nested' has a key 'parent' that points back to the original dictionary a, so the identity check is True.
❓ Predict Output
expert2:00remaining
Output of printing a self-referencing list
What is the output of this Python code?
Python
lst = [1, 2] lst.append(lst) print(lst)
Attempts:
2 left
💡 Hint
Python shows self-references in lists with [...].
✗ Incorrect
When printing a list that contains itself, Python shows the self-reference as [...], so the output is [1, 2, [...]].