Module 21 · Types and memory · Drill
Types and memory drill
Scenario prompts that check whether types and memory is usable under pressure. Answer out loud, then compare.
0 of 4 graded
Prompt 1
A Java method applyDiscount(Cart cart) mutates the cart's items and then sets cart = new Cart() before returning. The caller sees the discounted items but not the empty cart. Explain exactly why, then redesign Cart so this whole category of surprise cannot happen.
Prompt 2
A Java service's memory grows for days until it is killed. Heap dumps show millions of RequestContext objects referenced from a static ConcurrentHashMap keyed by request id, plus listener objects registered on an event bus by short-lived components. Explain why the garbage collector cannot help and fix both leaks.
Prompt 3
Design a generic Result type for a codebase that currently returns null or throws for failures. Show its type parameters, explain how Java's erasure limits the design compared with C# or Rust, and explain whether a Result of Dog should be usable where a Result of Animal is expected.
Prompt 4
A deduplication job keeps a HashSet of Customer objects and still produces duplicates. Customer overrides equals to compare normalised email but not hashCode; a later fix added hashCode over all fields; and the job lower-cases the email after inserting customers into the set. Explain every bug and the correct design.