Skip to content
Sharp

Core Java · interview question

Which of the following loop guards is necessary to avoid an off-by-one error in a loop that processes each item in an array?

Multiple choiceMediumchapter-1-curatedpremiumcore-javaloops-and-iteration
Choose one answer
Answer and explanation

Correct answer

A. I < array.length

Why

Using 'i < array.length' ensures that the loop stops before trying to access an index equal to the array's length, which would be out of bounds.

Test yourself on Core Java

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

Start free test
All Core Java questions →