Skip to content
Sharp

Core Java · interview question

What will the following code output if 'prices' is an empty array?

Multiple choiceEasychapter-1-curatedpremiumcore-javaloops-and-iteration
java
int[] prices = {}; int total = 0; for (int price : prices) { total += price; } System.out.println("Total: " + total);
Choose one answer
Answer and explanation

Correct answer

B. Total: 0

Why

The loop does not execute because the array is empty, so the initial value of total (0) is printed.

Test yourself on Core Java

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

Start free test
All Core Java questions →