Skip to content
Sharp

Object-oriented programming · interview question

What issue might arise if the constructor of a class sets a mutable object field directly instead of a defensive copy?

Multiple choiceHardchapter-2-curatedpremiumoopconstructors
Choose one answer
Answer and explanation

Correct answer

C. Object fields can be modified externally

Why

If a constructor sets a mutable object field directly, the object's state can be changed externally, leading to potential bugs and inconsistency in object state.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →