Skip to content
Sharp

Object-oriented programming · interview question

Given the following code snippet, what is the issue with the 'changeQuantity' method?

Multiple choiceHardchapter-3-curatedpremiumoopcontrolled-mutation
java
public void changeQuantity(int quantity) { this.quantity = quantity; }
Choose one answer
Answer and explanation

Correct answer

D. It allows negative quantities, leading to invalid state

Why

The method allows setting a negative quantity, which can lead to an invalid state for the product. Validation should ensure that only valid quantities (e.g., non-negative) are set.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →