Skip to content
Sharp

Core Java · interview question

Consider a method that uses a for loop to calculate the total price of items. What is the scope of a loop control variable in this context?

Multiple choiceMediumchapter-1-curatedpremiumcore-javascope-and-reassignment
java
for(int i = 0; i < items.length; i++) { total += items[i].price; }
Choose one answer
Answer and explanation

Correct answer

A. Only within the loop.

Why

In Java, a loop control variable is only accessible within the loop where it is declared. It helps prevent unintended use of the variable outside its intended context.

Test yourself on Core Java

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

Start free test
All Core Java questions →