Java OCA OCP Practice Question 1917

Question

What will the following program print?

public class Main{
    public static void main (String args []){
        int c = 0;
        boolean flag = true;
        for (int i = 0; i < 3; i++){
            while (flag){
                c++;/*from   w ww . java2  s. c o m*/
                if (i>c  || c>5) flag = false;
             }
         }
        System.out.println (c);
     }
}

Select 1 option

  • A. 3
  • B. 4
  • C. 5
  • D. 6
  • E. 7


Correct Option is  : D

Note

In the first iteration of for loop, the while loop keeps running till c becomes 6.

Now, for all next for loop iteration, the while loop never runs as the flag is false.

So final value of c is 6.




PreviousNext

Related