OCA Java SE 8 Mock Exam 2 - OCA Mock Question 29








Question

What's the output of the following code?

public class Main {
  public static void main(String[] args) {
    int i = 10;
    do
      while (i < 15)
        i = i + 20;
    while (i < 2);
    System.out.println(i);
  }
}
  1. 10
  2. 30
  3. 31
  4. 32




Answer



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.

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

public class Main {
  public static void main(String[] args) {
    int i = 10;// w  ww .jav  a 2 s  .com
    do
      while (i < 15)
        i = i + 20;
    while (i < 2);
    System.out.println(i);
  }
}