Java OCA OCP Practice Question 189

Question

Given:

4. public class Main { 
5.   public static void main(String[] args) { 
6.     String s = ""; 
7.     boolean b1 = true; 
8.     boolean b2 = false; 
9.     if((b2 = false) | (21%5) > 2) s += "x"; 
10.     if(b1 || (b2 == true))        s += "y"; 
11.     if(b2 == true)                s += "z"; 
12.     System.out.println(s); //from  w  w w  . j av a2 s. co m
13.   } 
14. } 

Which are true? (Choose all that apply.)

  • A. Compilation fails
  • B. x will be included in the output
  • C. y will be included in the output
  • D. z will be included in the output
  • E. An exception is thrown at runtime


C is correct.

Note

Line 9 uses the modulus operator, which returns the remainder of the division, which in this case is 1.

Line 9 sets b2 to false, and it doesn't test b2's value.

Line 10 sets b2 to true, and it doesn't test its value.

The short-circuit operator keeps the expression b2 = true from being executed.




PreviousNext

Related