Given the code fragment:
public static void main(String[] args) {
String [] table = {“aa”, “bb”, “cc”};
int ii = 0;
for (String ss : table) {
while (ii < table.length) {
System.out.println(ii);
ii++;
break;
}
}
}
How many times is 2 printed?
A.
zero
B.
once
C.
twice
D.
thrice
E.
it is not printed because compilation fails
Explanation:
The outer loop will run three times, one time each for the elements in table. The break statement breaks the inner loop immediately each time.
2 will be printed once only.
Note: If the line int ii = 0; is missing the program would not compile.
B
+1 cauze break the for loop
The Answer is B.
The code prints:
0
1
2
B because the break just affects to the inner loop