How many times is 2 printed as a part of the output?

Given:
public class DoCompare1 {
public static void main(String[] args) {
String[] table = {“aa”, “bb”, “cc”};
for (String ss: table) {
int ii = 0;
while (ii < table.length) {
System.out.println(ss + “, ” + ii);
ii++;
}
}
How many times is 2 printed as a part of the output?

Given:
public class DoCompare1 {
public static void main(String[] args) {
String[] table = {“aa”, “bb”, “cc”};
for (String ss: table) {
int ii = 0;
while (ii < table.length) {
System.out.println(ss + “, ” + ii);
ii++;
}
}
How many times is 2 printed as a part of the output?

A.
Zero

B.
Once

C.
Twice

D.
Thrice

E.
Compilation fails.

Explanation:
The for statement, for (String ss: table), is executed one time for each of the three
elements in table. The while loop will print a 2 once for each element.
Output:
aa, 0
aa, 1
aa, 2
bb, 0
bb, 1
bb, 2
cc, 0
cc, 1
cc, 2



Leave a Reply 2

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


Rohit

Rohit

Class definition is not closed by “}”
Answer should be E ?

Tim

Tim

Actually, the code misses two “}” to be compiled successfully.