Skip to content
Sharp

Object-oriented programming · interview question

In terms of encapsulation, what is a potential problem with a public field in a class?

Multiple choiceMediumchapter-3-curatedpremiumoopaccess-modifiers-and-visibility
Choose one answer
Answer and explanation

Correct answer

B. It exposes the internal state directly

Why

A public field exposes the internal state of a class directly, breaking encapsulation. This can lead to issues where the state is changed unexpectedly, making it difficult to maintain class invariants.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →