Question 1
Topic: Java Class Design
Skill tested: Designs immutable classes with defensive copying and controlled field exposure.
A billing service stores a customer's mailing address when an invoice is created. The Address type is mutable, and the service must ensure later changes by either the caller or the invoice user cannot alter the address captured on the invoice. The address has two mutable components, and the Invoice API should not expose mutable internal state. Which design best meets these requirements while keeping Invoice instances immutable?
A final reference does not make the referenced Address immutable. Copying on construction prevents the caller from changing Invoice state through the original object; returning a copy prevents callers from mutating that state through the getter. A setter, as in C, means the Invoice is not immutable. Making the class final does not protect the mutable object returned by a getter, and assigning and returning the original reference in A leaves both mutation paths open.