0
0
Pythonprogramming~20 mins

Self reference in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Self Reference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
AKeyError
BFalse
CTypeError
DTrue
Attempts:
2 left
💡 Hint
Think about whether the dictionary stored inside the key 'self' is the same object as d.
Predict Output
intermediate
2:00remaining
Output of a self-referencing list
What will this Python code print?
Python
lst = [1, 2]
lst.append(lst)
print(len(lst))
A3
B2
CTypeError
DRecursionError
Attempts:
2 left
💡 Hint
Appending the list to itself adds one more element.
🔧 Debug
advanced
3: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()
AThe class Node is not instantiated properly
Bself.child is None causing AttributeError
Cget_child calls itself endlessly because self.child is self
DThe constructor __init__ is missing a return statement
Attempts:
2 left
💡 Hint
Look at what get_child returns and what self.child points to.
🧠 Conceptual
advanced
2: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)
AFalse
BTrue
CKeyError
DTypeError
Attempts:
2 left
💡 Hint
Check if the nested dictionary's 'parent' key points back to the original dictionary.
Predict Output
expert
2: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)
A[1, 2, [...]]
B[1, 2, [1, 2]]
CRecursionError
D[1, 2, None]
Attempts:
2 left
💡 Hint
Python shows self-references in lists with [...].