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

1. ( Looping Techniques ) Predict the output of the following program segment pu

ID: 3803825 • Letter: 1

Question

1.    ( Looping Techniques )

          Predict the output of the following program segment

                     public static void main(String args[])

            {

                  int i = 0;

                  do   

                  {    

                  System.out.print("i = " + i + " ");

                  }

                  while (i > 5);

            }                                                  

          (a)       i = 0                        (b)       i = 0 1 2 3 4 5 6

          (c)        i = 1 2 3 4 5 6           (d)       i = 1 2 3 4 5

2.    ( Looping Techniques )

          Predict the output of the following program segment.

                     public static void main(String args[])

            {

                  int i = 0;

                  do   

                  {    

                  System.out.print("i = " + i + " ");

                  i++;     

                  }

                  while (i > 5);

            }

                                                   

          (a)       i = 0                        (b)       i = 0 1 2 3 4 5 6

          (c)        i = 1 2 3 4 5 6           (d)       i = 1 2 3 4 5  

3.    ( Looping Techniques )

          Predict the output of the following program segment.

                     public static void main(String args[])

            {

                  int i = 5;

                  do   

                  {    

                  System.out.print("i = " + i + " ");

                  i++;     

                  }

                  while (i < 5);

            }                                                  

          (a)       i = 0                        (b)       i = 1 2 3 4 5 6

          (c)        i = 1 2 3 4 5             (d)       i = 5

Explanation / Answer

1) initially i is initialized to 0 ...and we are using do..while loop so the loop will be executed definitely once even if the condition evaluates to false.in our program the test condition is i>5 since this is false only i=0 will be printed.

therefore option a is correct

2) here again i is initialized to 0 so definirely i=0 will be printed and i is incremented but the condition evaluates to false.

so only i=0 will be printed therefore the correct option is a.

3) here i is initially 5 ...since it is a do while loop i=5 will be printed and i is incremented .

the test condition evaluates to false so the loop will not be executed again.therefore the correct option is d