Java OCA OCP Practice Question 2875

Question

Given this code in a method:

5.     int x = 3;  
6.     for(int i = 0; i < 3; i++) {  
7.        if(i == 1) x = x++;  
8.        if(i % 2 == 0 && x % 2 == 0) System.out.print(".");  
9.        if(i % 2 == 0 && x % 2 == 1) System.out.print("-");  
10.       if(i == 2 ^ x == 4) System.out.print(",");  
11.     }  
12.     System.out.println("<"); 

What is the result?

  • A. -.,<
  • B. --,<
  • C. -.,,<
  • D. --,,<
  • E. -.-,,<
  • F. Compilation fails.


B is correct.

Note

The % operator returns the remainder of a division.

The ^ (XOR) operator returns true only if exactly one of the expressions is true.

The x = x++; line doesn't leave x == 4 because the ++ is applied after the assignment has occurred.




PreviousNext

Related