Courseiva
Controlling Program FloweasyMultiple ChoiceObjective-mapped

1Z0-829 Controlling Program Flow Practice Question

A developer is designing a loop to iterate through an array of integers and stop processing when the value -1 is encountered. Which loop construct should be used?

⚠ Common exam trap

Many candidates confuse `break` with `continue` or mistakenly use `return` or `exit` to exit a loop, not realizing that `break` is the precise mechanism for loop termination without affecting the rest of the method or JVM.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

for(int i : array) { if(i == -1) break; }

The `break` statement immediately terminates the loop when the value -1 is encountered, which is exactly what the requirement specifies. In Java, `break` exits the innermost loop or switch statement, making it the appropriate control flow mechanism for stopping iteration upon a condition.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • for(int i : array) { if(i == -1) exit; }

    Why it's wrong here

    There is no exit keyword in Java; this is not valid syntax.

  • for(int i : array) { if(i == -1) return; }

    Why it's wrong here

    The return statement exits the method, which stops the loop but also exits the method, which may not be intended.

  • for(int i : array) { if(i == -1) break; }

    Why this is correct

    The break statement immediately exits the loop, which is the correct way to stop processing upon encountering -1.

  • for(int i : array) { if(i == -1) continue; }

    Why it's wrong here

    The continue statement skips the current iteration and continues with the next, so the loop does not stop.

About these practice questions

Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.