Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. What does each of the following program segment do? for (int i = 1; i <= 4; i

ID: 3758507 • Letter: 1

Question

1. What does each of the following program segment do?


   for (int i = 1; i <= 4; i++)
      for (int j =1; j <=3; j++)
        System.out.print("@@@");   
   System.out.println();

2. What does each of the following program segment do?

      for (int i = 1; i <= 4; i++)
      {
         for (int j =1; j <=3; j++)
              System.out.print("@@@");   
          System.out.println();
       }

3.

4.

5. What does each of the following program segment do?

       for (int i = 1; i <= 3; i++)
          for (int j =1; j <= 2; j++)
             for (int k =1; k <=3; k++)
                    System.out.println("@");

What does each of the following program segment do?

      for (int i = 1; i <= 4; i++)
           for (int j =1; j <=3; j++)
          {
               System.out.print("@@@");   
   System.out.println();
          }

Explanation / Answer

1.

@@@ will be printed 12 times

(4 outer loop * 3 inner loop)

output is:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

2.

Each line will have @@@ printed 3 times because of inner for loop and println statement is fired after each inner for loop,

Hence 4 lines will be printed because of outer loop.

output is:

@@@@@@@@@

@@@@@@@@@

@@@@@@@@@

@@@@@@@@@

3.

In Inner for loop after the set @@@ is printed, there is a println statement.

So 12 lines will be printed with @@@ on each line.

output is:

@@@

@@@

@@@

@@@

@@@

@@@

@@@

@@@

@@@

@@@

@@@

@@@

4. Question is empty.

5.

Innermost for loop prints @ on each line.

Innermost for loop prints 3 lines.

Considering outer for loop and middle for loop , innermost for loop will be executed 6 times.

Total lines = 6 * 3 = 18 lines

output is:

@

@

@

@

@

@

@

@

@

@

@

@

@

@

@

@

@

@

++++++++++++++++++++++++++