Skip to content
Sharp

Core Java · interview question

Which loop structure would be most appropriate for applying a discount to each item in a cart when the number of items is unknown?

Multiple choiceMediumchapter-1-curatedpremiumcore-javaloops-and-iteration
Choose one answer
Answer and explanation

Correct answer

D. For (int i = 0; i < cart.length; i++)

Why

A 'for' loop with a known end condition (cart.length) is best when processing each element of an array, even if the array's length is unknown initially.

Test yourself on Core Java

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

Start free test
All Core Java questions →