OCA Java SE 8 Operators/Statements - OCA Mock Question Operator and Statement 5








Question

  • What is the output of the following application?
     1: public class Main { 
     2:   public static void main(String[] args) { 
     3:     int x = 0; 
     4:     while(x++ < 5) {} 
     5:     String message = x > 5 ? "Greater than" : false; 
     6:     System.out.println(message+","+x); 
     7:   } 
     8: } 
  1. Greater than,5
  2. false,5
  3. Greater than,6
  4. false,6
  5. The code will not compile because of line 4.
  6. The code will not compile because of line 5.




Answer



F.

Note

The ternary operator has two expressions, one of them is of type String and the other is of boolean value.

The ternary operator allows to unmatching types.

The assignment of the ternary operator is asking for the String reference.