Skip to content
Sharp

Object-oriented programming · interview question

Why do teams use List.copyOf when returning a collection from a method in production code?

Multiple choiceMediumchapter-3-curatedpremiumoopdefensive-copying
Choose one answer
Answer and explanation

Correct answer

C. It creates an immutable copy of the original list

Why

List.copyOf creates an immutable copy of the provided list, ensuring that the caller cannot modify the internal state of the object by altering the returned collection. This helps maintain encapsulation.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →