1Z0-829 Nested loops Practice Question
Exhibit
Refer to the exhibit.
public class LoopTest {
public static void main(String[] args) {
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i + j > 3) {
break outer;
}
System.out.print(i + j + " ");
}
}
}
}Given the following Java code:
```java
for (int i = 0; i <= 2; i++) {
for (int j = i; j <= i+2; j++) {
if (j <= 3) {System.out.print(j + " ");
}
}
}```
What is the output?
⚠ Common exam trap
The trap here is that candidates miscount the number of iterations or misjudge the inner loop's termination condition, often thinking the inner loop runs from 0 to i+2 instead of from i to i+2, leading to sequences like 0 1 2 0 1 2 0 1 2 or adding extra values.
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
✓
0 1 2 1 2 3 2 3
The program uses nested for loops. The outer loop iterates i from 0 to 2 (inclusive). For each i, the inner loop iterates j from i to i+2 (inclusive), but due to a condition that prevents printing values greater than 3, the inner loop effectively prints j only up to 3. Thus, for i=0 it prints 0,1,2; for i=1 it prints 1,2,3; for i=2 it prints 2,3. Combined on one line, the output is '0 1 2 1 2 3 2 3', which corresponds to option B.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
0 1 2 1 2 3 2 3 4
Why it's wrong here
4 is not printed because break outer occurs before printing.
- ✓
0 1 2 1 2 3 2 3
Why this is correct
Correct sequence as per execution.
- ✗
0 1 2 1 2 3 2 3 4 5
Why it's wrong here
Includes extra numbers.
- ✗
0 1 2 1 2 3 2 3 3 4
Why it's wrong here
Extra numbers from inner loop.
Go deeper
Related to this question
About these practice questions
One of 513 original 1Z0-829 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
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.