0
0
Compiler Designknowledge~10 mins

Garbage collection basics in Compiler Design - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to identify unreachable objects in a heap.

Compiler Design
unreachable_objects = [obj for obj in heap if obj not in [1]]
Drag options to blanks, or click blank then click option'
Avariables
Bstack
Cheap
Droots
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing heap with roots
Using stack instead of roots
2fill in blank
medium

Complete the code to mark reachable objects during garbage collection.

Compiler Design
def mark(obj):
    if obj not in marked:
        marked.add(obj)
        for ref in obj.[1]:
            mark(ref)
Drag options to blanks, or click blank then click option'
Areferences
Bchildren
Cpointers
Dlinks
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'children' which is not a standard term
Using 'links' which is ambiguous
3fill in blank
hard

Fix the error in the code to sweep unmarked objects from the heap.

Compiler Design
for obj in heap[:]:
    if obj not in [1]:
        heap.remove(obj)
Drag options to blanks, or click blank then click option'
Astack
Broots
Cmarked
Dvisited
Attempts:
3 left
💡 Hint
Common Mistakes
Removing objects not in roots (too broad)
Using stack which is unrelated here
4fill in blank
hard

Fill both blanks to implement reference counting update after object assignment.

Compiler Design
def assign(obj, new_ref):
    obj.ref_count [1] 1
    new_ref.ref_count [2] 1
Drag options to blanks, or click blank then click option'
A-=
B+=
C*=
D/=
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing old ref count
Decreasing new ref count
5fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps objects to their reference counts if count is greater than zero.

Compiler Design
ref_counts = {obj[1]: obj.ref_count : obj.ref_count for obj in heap if obj.ref_count [2] 0}
Drag options to blanks, or click blank then click option'
A.id
B:
C>
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '>' in condition
Using ':' incorrectly in comprehension