Skip to content
Sharp

Core Java · interview question

What will be the output of the following code snippet if the cart contains item prices {15, 25, 30}?

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

Correct answer

D. Total: 70

Why

The loop iterates through each item price in the cart and adds them to the total. The sum of 15, 25, and 30 is 70.

Test yourself on Core Java

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

Start free test
All Core Java questions →