Skip to content
Sharp

Core Java · interview question

In an e-commerce app, you have a class Order with a method calculateTotal(). The method uses variables defined in the method body. What is a sign that some of these variables should become fields?

Multiple choiceHardchapter-2-curatedpremiumcore-javacode-reading-and-refactoring-to-objects
Choose one answer
Answer and explanation

Correct answer

A. The variables represent the state of the order.

Why

If variables represent the state of the order, they should be fields rather than method-local variables. Fields help encapsulate the state of an object, making it easier to manage and understand.

Test yourself on Core Java

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

Start free test
All Core Java questions →