Preview

25 - Nested Iteration

 1. The number of times a nested for loop body is executed is the number of times the ________ executes times the number of times the inner loop executes

  first if statement

  inner loop

  outer loop

  boolean condition

 2. In the following example, the outer loop executes 4-0+1= 5 times and the inner 9-0+1=10 times so the total is 5 * 10 = 50.
public class NestedLoops
{

   public static void main(String[] args)
   {
       for (int row = 0; row < 5; row++)
       {
           for (int col = 0; col < 10; col++)
           {
               System.out.print("*");
           }
           System.out.println();
       }
   }
}

  FALSE

  TRUE

 3. How many times does the following code print a *?
for (int i = 3; i < 8; i++)
{
    for (int y = 1; y < 5; y++)
    {
        System.out.print("*");
    }
    System.out.println();
}

  40

  24

  20

  30

 4. What does the following code print?
for (int i = 2; i < 8; i++)
{
    for (int y = 1; y <= 5; y++)
    {
        System.out.print("*");
    }
    System.out.println();
}

   A rectangle of 6 rows with 5 stars per row.

  A rectangle of 8 rows with 4 stars per row.

  A rectangle of 8 rows with 5 stars per row.

  A rectangle of 6 rows with 4 stars per row.

 5. What does the following code print?
for (int i = 3; i <= 9; i++)
{
   for (int j = 6; j > 0; j--)
   {
       System.out.print("*");
   }
   System.out.println();
}

   A rectangle of 6 rows and 6 stars per row.

  A rectangle of 7 rows and 6 stars per row.

  A rectangle of 9 rows and 5 stars per row.

  A rectangle of 7 rows and 5 stars per row.