Questions: Escape Analysis for Allocation Optimization

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

A Java method creates a Rectangle object, computes its area, and returns only the integer area. Escape analysis determines the Rectangle is non-escaping. Which optimization is the compiler most likely to apply?

AThe object is allocated on the heap but with a reduced header to lower GC pressure
BScalar replacement: the Rectangle's fields become individual local variables (or CPU registers), eliminating the object allocation entirely
CThe garbage collector is notified immediately so it can collect the object before the function returns
DThe constructor is inlined to reduce the number of heap allocation calls from two to one
Question 2 Multiple Choice

An object is passed to a virtual method call on an interface type. Why does the compiler typically conservatively assume this object escapes?

AVirtual methods always store their arguments in static fields for dispatch table lookup
BThe compiler cannot see through the interface — it cannot prove which implementation will run and whether that implementation stores the reference beyond the call
CAll objects passed to methods must be heap-allocated to ensure they are accessible across the call stack
DInterface dispatch requires placing the object in a shared memory region visible to all threads
Question 3 True / False

Stack allocation is faster than heap allocation primarily because the stack resides in faster physical memory (L1 cache) while the heap uses slower main memory.

TTrue
FFalse
Question 4 True / False

An object that is returned from a function always escapes that function's scope and therefore cannot be stack-allocated.

TTrue
FFalse
Question 5 Short Answer

What does it mean for an object to 'escape' a function, and why must escape analysis be conservative when it cannot determine escape status?

Think about your answer, then reveal below.