Skip to content
Sharp

Core Java · interview question

Which of the following practices best helps maintain invariants in a Java class?

Multiple choiceMediumchapter-3-curatedpremiumcore-javacode-reading-and-state-safety
Choose one answer
Answer and explanation

Correct answer

B. Validating inputs in setters and constructors.

Why

Validating inputs in setters and constructors ensures that only valid data is set on the object, which helps maintain the class invariants and ensures consistent state.

Test yourself on Core Java

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

Start free test
All Core Java questions →