Skip to content
Sharp

Core Java · interview question

What is the result of the following code if 'prices' is an array of integers and initially 'total' is zero? while (index < prices.length) { total += prices[index]; index++; }?

Multiple choiceEasychapter-1-curatedpremiumcore-javaloops-and-iteration
java
while (index < prices.length) { total += prices[index]; index++; }
Choose one answer
Answer and explanation

Correct answer

A. The total of all prices

Why

The loop iterates over each element in the 'prices' array, adding each element's value to 'total'. When 'index' equals 'prices.length', the loop exits, resulting in 'total' being the sum of all prices.

Test yourself on Core Java

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

Start free test
All Core Java questions →