Skip to content
Sharp

Object-oriented programming · interview question

Why is it generally a bad idea to expose a class's internal fields as public?

Multiple choiceMediumchapter-3-curatedpremiumoopencapsulation
Choose one answer
Answer and explanation

Correct answer

A. It breaks encapsulation by allowing uncontrolled access

Why

Having public fields allows external code to change an object's state without validation, breaking the encapsulation principle and potentially leading to inconsistent or invalid states.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →