Sunday, August 29, 2010

The ^ operator returns true when EXACTLY one operand is true

Courtesy of Sun Web Learning Center


Given:

 3. public class Doctor {
 4.   public static void main(String[] args) {
 5.     String s = "";
 6.     int x = 2;
 7.     if((7 > (4*2)) ^ (5 != 4))    s += "1st ";
 8.     if((5 < 7) ^ (7 > 5))         s += "2nd ";
 9.     if(((4 * x++) < 9) ^ (x > 3)) s += "3rd ";
10.     System.out.println(s);
11.   }
12. }

What is the result?


A 1st
B 2nd
C 3rd
D 1st 2nd
E 1st 3rd
F 2nd 3rd
G No output is produced

E is correct. The ^ operator returns true when exactly one operand is true.

No comments:

Post a Comment