Skip to content
Sharp

Core Java · interview question

What will be the output if the 'discounts' array is empty in the following loop?

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

Correct answer

C. Total discount: 0

Why

Since the array is empty, the loop doesn't execute, and the total discount retains its initial value of 0.

Test yourself on Core Java

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

Start free test
All Core Java questions →