Skip to content
Sharp

Object-oriented programming · interview question

Consider a class representing a customer's order in an e-commerce application. Which of the following practices helps ensure the class is immutable?

Multiple choiceMediumchapter-3-curatedpremiumoopimmutability-basics
Choose one answer
Answer and explanation

Correct answer

D. Use final fields and provide no setters.

Why

Using final fields and not providing setters ensures that once the order object is created, its state cannot change. This adheres to the principles of immutability, making the object stable and predictable.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →