Recall & Review
beginner
What is a nested for loop in Java?
A nested for loop is a for loop inside another for loop. It lets you repeat actions in a grid or table-like way, like rows and columns.
Click to reveal answer
beginner
How many times does the inner loop run if the outer loop runs 3 times and the inner loop runs 4 times each time?
The inner loop runs 4 times for each of the 3 outer loop runs, so 3 × 4 = 12 times in total.
Click to reveal answer
beginner
What is the output of this code?
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 3; j++) {
System.out.print(i + "-" + j + " ");
}
System.out.println();
}The output is:
1-1 1-2 1-3
2-1 2-2 2-3
This shows the outer loop number paired with each inner loop number.
Click to reveal answer
beginner
Why do we use nested for loops?
We use nested for loops to handle tasks that need multiple levels of repetition, like printing tables, grids, or working with multi-dimensional arrays.
Click to reveal answer
intermediate
What happens if you swap the inner and outer loops in a nested for loop?
Swapping loops changes the order of repetition. The inner loop becomes the outer loop and vice versa, which can change the output pattern or how data is processed.
Click to reveal answer
How many times will the inner loop run in this code?
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 2; j++) {
// inner loop body
}
}✗ Incorrect
The inner loop runs 2 times for each of the 5 outer loop runs, so 5 × 2 = 10 times.
What is the purpose of the inner loop in a nested for loop?
✗ Incorrect
The inner loop repeats its actions multiple times for each time the outer loop runs.
Which of these is a correct nested for loop structure in Java?
✗ Incorrect
Option A shows a proper nested for loop with one for loop inside another.
What will this code print?
for (int i=1; i<=2; i++) {
for (int j=1; j<=2; j++) {
System.out.print(i*j + " ");
}
}✗ Incorrect
The output is 1*1=1, 1*2=2, 2*1=2, 2*2=4 printed in sequence.
If the outer loop runs 4 times and the inner loop runs 3 times, how many total iterations happen?
✗ Incorrect
Total iterations = outer loop count × inner loop count = 4 × 3 = 12.
Explain what a nested for loop is and give a simple example in Java.
Think about loops inside loops and how they repeat actions.
You got /3 concepts.
Describe a real-life situation where you might use a nested for loop.
Imagine rows and columns in a spreadsheet or seats in a theater.
You got /3 concepts.