Skip to content
Sharp

Object-oriented programming · interview question

Given the constructor public Order(String id, List<Item> items), what is a potential design improvement?

Multiple choiceMediumchapter-2-curatedpremiumoopconstructors
Choose one answer
Answer and explanation

Correct answer

D. Initialize items to a new ArrayList.

Why

Initializing 'items' to a new ArrayList can prevent accidental modification of the list passed to the constructor, enhancing encapsulation by not exposing internal structure.

Test yourself on Object-oriented programming

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

Start free test
All Object-oriented programming questions →