Questions: Garbage Collection Algorithms

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

A program creates thousands of short-lived temporary string objects during text processing, discarding most of them within milliseconds. Which garbage collection strategy is best suited to this workload, and why?

AMark-and-sweep, because it handles variable-sized objects cleanly by freeing each one individually.
BGenerational collection, because most strings will be collected cheaply in frequent nursery passes without ever reaching the older generation.
CReference counting, because each string is freed immediately when its reference count drops to zero.
DMark-and-compact, because the many small freed objects would otherwise cause severe heap fragmentation.
Question 2 Multiple Choice

Object A holds a reference to object B, and object B holds a reference back to A. No other live variables reference either A or B. Under which memory management approach will A and B be reclaimed without programmer intervention?

AReference counting, because each object's count drops to zero when the other is freed first.
BReference counting, but only if a separate cycle-detection pass is added.
CAny tracing garbage collector (mark-and-sweep, copying, generational), because reachability from roots determines garbage regardless of internal reference cycles.
DNo automatic strategy can reclaim them; the programmer must break the cycle manually.
Question 3 True / False

Copying collection is inefficient when most objects are short-lived, because it should copy most those short-lived objects before reclaiming their space.

TTrue
FFalse
Question 4 True / False

In generational garbage collection, write barriers are needed to track old-to-young references so that nursery collections can find all roots without scanning the entire old generation.

TTrue
FFalse
Question 5 Short Answer

Explain why copying collection's cost is proportional to what survives rather than to what dies, and why this makes it efficient for short-lived objects.

Think about your answer, then reveal below.