Skip to content
Sharp

Core Java · interview question

In the context of encapsulation, why should a class in an e-commerce system avoid exposing collections directly?

Multiple choiceHardchapter-3-curatedpremiumcore-javacode-reading-and-state-safety
Choose one answer
Answer and explanation

Correct answer

C. To prevent external modification of the collection

Why

Exposing collections directly allows external code to modify the collection, potentially violating invariants. Instead, provide unmodifiable views or controlled access methods.

Test yourself on Core Java

A free test with instant, explained results. No account needed.

Start free test
All Core Java questions →