Java OCA OCP Practice Question 2790

Question

Given:

public class Main {
   public static void main(String[] args) {
      int x = 5;//from  ww  w  . j a  va  2  s .  c o m
      int y = (x < 6) ? 7 : 8;
      System.out.print(y + " ");
      boolean b = (x < 6) ? false : true;
      System.out.println(b + " ");
      assert (x < 6) : "bob";
      assert (((x < 6) ? false : true)) : "fred";
   }
}

And, if the code compiles, the invocation:.

java -ea Main 

Which will be contained in the output? (Choose all that apply.)

  • A. 7
  • B. 8
  • C. bob
  • D. fred
  • E. true
  • F. false
  • G. Compilation fails.


A, D, and F are correct.

Note

The ternary operator assigns the first value when the expression is true, so y == 7, and b == false.

The second assert statement has a ternary operator as an expression and since the result is false, fred is added to the AssertionError stack trace.




PreviousNext

Related