Skip to content
Sharp

Core Java · interview question

How can the following loop be optimized to stop early if a price over $100 is found?

Multiple choiceHardchapter-1-curatedpremiumcore-javaloops-and-iteration
java
int[] prices = {20, 50, 150, 80}; for (int price : prices) { if (price > 100) break; }
Choose one answer
Answer and explanation

Correct answer

A. Add a break statement inside the if condition.

Why

A break statement inside the if condition allows the loop to exit early when a price over $100 is found, improving efficiency.

Test yourself on Core Java

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

Start free test
All Core Java questions →