Java OCA OCP Practice Question 2427

Question

What's the output of the following code?

public class Main { 
    public static void main(String[] args) { 
        int i = 10; 
        do //  w w w  .ja  v a2s .c  o m
            while (i < 15) 
                i = i + 20; 
        while (i < 2); 
        System.out.println(i); 
    } 
} 
  • a 10
  • b 30
  • c 31
  • d 32


b

Note

The condition specified in the do-while loop evaluates to false (because 10<2 evaluates to false).

But the control enters the do-while loop because the do-while loop executes at least once-its condition is checked at the end of the loop.

The while loop evaluates to true for the first iteration and adds 20 to i, making it 30.

The while loop doesn't execute for the second time.

Hence, the value of the variable i at the end of the execution of the previous code is 30.




PreviousNext

Related