Skip to content
Sharp

Object-oriented programming · interview question

How can defensive copying help in protecting internal state of a class?

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

Correct answer

A. By returning a copy of mutable objects instead of the original

Why

Defensive copying involves creating a copy of a mutable object and returning that copy instead of the original. This prevents clients from modifying the internal state of the class.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →