Courseiva
Controlling Program FlowmediumMultiple SelectObjective-mapped

Java Loop Exit Mechanisms

Which two of the following are valid ways to exit a loop in Java?

Quick Answer

The answer is the break statement, including its labeled form, which is a valid way to exit a loop in Java. A labeled break allows you to terminate an outer loop from within a nested loop by specifying the outer loop's label, as defined in the Java Language Specification (JLS §14.15). This is distinct from an unlabeled break, which only exits the innermost loop. On the Oracle Certified Professional Java SE 17 Developer 1Z0-829 exam, this concept tests your understanding of control flow in nested structures, often appearing in questions that ask you to identify valid loop exit mechanisms or predict output. A common trap is confusing labeled break with continue or assuming break cannot target an outer loop—remember that the label must be placed immediately before the loop statement. Memory tip: think of a labeled break as a "teleport" that jumps out of any loop you name, not just the one you're inside.

⚠ Common exam trap

It's easy for candidates to confuse 'continue' with 'break' or think that System.exit(0) or throwing an exception are valid loop exit mechanisms, when in fact only 'break' (and labeled break) are designed specifically for that purpose.

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

label break

A labeled break statement allows you to exit an outer loop from within a nested loop by specifying the label of the outer loop. This is a valid control flow mechanism in Java, as defined in the Java Language Specification (JLS §14.15).

Answer analysis

Option-by-option breakdown

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

  • label break

    Why this is correct

    A labeled break can exit a specific outer loop.

  • continue

    Why it's wrong here

    continue only skips to the next iteration, it does not exit the loop.

  • break

    Why this is correct

    break immediately exits the innermost loop.

  • System.exit(0)

    Why it's wrong here

    System.exit terminates the entire JVM, not just the loop.

  • throw new Exception()

    Why it's wrong here

    throw can cause the loop to exit if uncaught, but it is not a loop control statement.

About these practice questions

This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on 1Z0-829

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. Refer to the exhibit. What is the output when the program is executed?

medium
  • A.0 0 0 1 0 2 1 0 1 1 1 2
  • B.0 0 0 1 0 2 1 0 1 1 2 0 2 1 2 2
  • C.0 0 0 1 0 2 1 0 1 1
  • D.0 0 0 1 0 2 1 0 1 1 1 2 2 0 2 1 2 2

Why C: The code uses nested for loops with break labels. The outer loop iterates i from 0 to 2, and the inner loop iterates j from 0 to 2. When i == 1 and j == 1, the break outer; statement exits both loops, so after printing 0 0, 0 1, 0 2, 1 0, 1 1, the program terminates. This matches option C.

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.