How many times is 2 printed?

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?

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.



Leave a Reply 0

Your email address will not be published. Required fields are marked *