Core Java · interview question
In a class Inventory, you notice several methods that manipulate a List<Product>. When refactoring, what is a better object-oriented approach?
Multiple choiceHardchapter-2-curatedpremiumcore-javacode-reading-and-refactoring-to-objects
Answer and explanation
Correct answer
C. Create a separate ProductCollection class to manage the list.
Why
Creating a separate ProductCollection class encapsulates the operations and management of the product list, adhering to the single responsibility principle. It leads to more structured and maintainable code.
Test yourself on Core Java
A free test with instant, explained results. No account needed.
More Core Java questions
- What does executing a throw statement inside a catch block evaluate to?
- In Java, what happens if an exception is thrown in a method and not caught within that method?
- Which method would you use to add an element to a List at a specific position?
- Consider the following code snippet. What is the issue with the 'applyDiscount' method?
- How do you correctly initialize a BigDecimal to represent the monetary value 50.75?
- Why might using == with Integer objects sometimes return false even if they have the same value?