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








Question

What's the output of the following code?

public class Main {
  public static void main(String[] args) {
    int a = 10;
    if (a++ > 10) {
      System.out.println("true");
    }
    {
      System.out.println("false");
    }
    System.out.println("ABC"); 
  }
}


    a  true 
       false 
       ABC 

    b  false 
       ABC 

    c  true 
       ABC 

    d  Compilation error 




Answer



B

Note

The code has no compilation errors.

The following code snippet isn't part of the if construct:

{ 
    System.out.println("false"); 
} 

Therefore the value false will print no matter what.

public class Main {
  public static void main(String[] args) {
    int a = 10;//w w w .  ja v  a  2  s .  com
    if (a++ > 10) {
      System.out.println("true");
    }
    {
      System.out.println("false");
    }
  }
}

The code above generates the following result.