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 4

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


Jazz

Jazz

B

Fredy Jimenez Rendon

Fredy Jimenez Rendon

+1 cauze break the for loop

James

James

The Answer is B.

The code prints:
0
1
2

carmen

carmen

B because the break just affects to the inner loop