Skip to content
Sharp

Object-oriented programming · interview question

Consider the following code snippet. What is a potential issue with the 'updatePrice' method?

Multiple choiceHardchapter-3-curatedpremiumoopcontrolled-mutation
java
public void updatePrice(double newPrice) { this.price = newPrice; }
Choose one answer
Answer and explanation

Correct answer

C. It lacks validation, potentially allowing invalid prices.

Why

The 'updatePrice' method should include validation to ensure that the new price meets any necessary conditions (e.g., not negative or zero), preventing invalid state changes.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →