Multiple choice

How many times is “aa” printed as a part of output?

public class ScopeTest
{
int z;
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++;
}
}
}
}

  1. Zero times

  2. Once

  3. Twice

  4. Thrice

  5. Compilation fails

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

This is the correct choice. The for statement, for (String ss: table), is executed one time for each of the three elements in table. The output comes out to be : aa, 0aa, 1aa, 2bb, 0bb, 1bb, 2cc, 0cc, 1cc, 2. So, “aa” will be printed 3 times, hence this answer is correct.