Multiple choice

How many times is 1 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. For statement, (String ss: table), is executed one time for each of the three elements in table. The while loop will print 1 once for each element. The output comes out to be : aa, 0aa, 1aa, 2bb, 0bb, 1bb, 2cc, 0cc, 1cc, 2 So, 1 will be printed 3 times, hence this answer is correct.