What is a nested loop in Java?
A
A loop inside a method B
Two loops with the same condition C
A loop inside another loop D
A loop that calls another loop
Analysis & Theory
A nested loop is a loop written inside the body of another loop.
How many times will the inner loop run?
```java
for(int i=0; i<2; i++) {
for(int j=0; j<3; j++) {
System.out.print("*");
}
}```
A
2 B
3 C
5 D
6
Analysis & Theory
Outer loop runs 2 times, inner runs 3 times → 2 × 3 = 6.
What will this print?
```java
for(int i=1; i<=2; i++) {
for(int j=1; j<=2; j++) {
System.out.print(i + j);
}
}```
A
2334 B
2345 C
234 D
1234
Analysis & Theory
Prints sums: (1+1)(1+2)(2+1)(2+2) → 2,3,3,4 → 2334.
Which of the following is a valid nested loop?
A
for(i=0;i<3;i++) if(j<3) {} B
for(int i=0;i<3;i++) while(j<3) {} C
while(i<3) for(j=0;j<3;j++) {} D
All of the above
Analysis & Theory
All options show valid nesting of different loop types.
What is printed?
```java
for(int i=1; i<=2; i++) {
for(int j=1; j<=3; j++) {
System.out.print("#");
}
System.out.println();
}```
A
### B
###### C
###\n### D
#\n#\n#
Analysis & Theory
Each row prints 3 `#`, with line breaks after each outer loop iteration.
Can a `while` loop be nested inside a `for` loop?
A
No B
Only with arrays C
Yes D
Only in methods
Analysis & Theory
Yes, any type of loop (`for`, `while`, `do-while`) can be nested inside each other.
What will be printed?
```java
for(int i=0; i<2; i++) {
for(int j=0; j<2; j++) {
if(j == 1) break;
System.out.print("*");
}
}```
A
** B
**** C
* D
**\n**
Analysis & Theory
Each inner loop prints one `*` then breaks → two `*` total.
Which keyword exits only the inner loop?
A
stop B
return C
break D
continue
Analysis & Theory
`break` exits only the loop where it's placed (usually the inner one).
What will be printed?
```java
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 2; j++) {
continue;
System.out.print("X");
}
}```
A
XX B
X C
Nothing D
Error
Analysis & Theory
`continue` skips the print statement in every iteration → prints nothing.
How many times will 'Hi' be printed?
```java
int i = 0;
while(i < 2) {
int j = 0;
while(j < 3) {
System.out.println("Hi");
j++;
}
i++;
}```
A
3 B
2 C
5 D
6
Analysis & Theory
Outer loop: 2 times × inner loop: 3 times = 6 prints of 'Hi'.