Quiz yourself: Bitwise boolean operators in Java

December 20, 2021 | 4 minute read
Text Size 100%:

When evaluating expressions, which boolean operator comes first: AND, OR, or XOR?

Download a PDF of this article
More quiz questions available here

Given this Prophet class

Copied to Clipboard
Error: Could not Copy
Copied to Clipboard
Error: Could not Copy
public class Prophet {  
  static boolean bad() {
    System.out.print("bad ");
    return false;
  }
  static boolean scary() {
    System.out.print("scary ");
    return true;
  }  
  static boolean funny() {
    System.out.print("funny ");
    return false;
  }  
  static boolean lucky() {
    System.out.print("lucky ");
    return true;
  }  
  public static void main(String[] args) {
    var happy = bad() | funny() & scary() ^ lucky();
    System.out.print(happy);
  }  
}

What is the output? Choose one.

A. funny scary lucky bad true
B. bad funny scary lucky true
C. bad funny scary lucky false
D. scary lucky false
E. bad funny false

Answer. This question investigates the order of operand evaluation and the precedence of boolean operators. Start by noticing that the sample code in this question uses regular bitwise boolean operators—not the short-circuit logical operators. The following are the bitwise boolean operators used in this question:

  • The AND (&) operator returns 1 if both operands are 1; otherwise & returns 0.
  • The OR (|) operator returns 1 if either operand is 1; otherwise | returns 0.
  • The XOR (^) operator returns 1 if exactly one operand is 1; otherwise ^ returns 0.

For completeness, the following are the short-circuit logical operators not used in this question:

  • The short-circuit AND (&&) operator doesn’t bother to evaluate the right side of the AND expression if the left side is false.
  • Similarly, the short-circuit OR (||) operator doesn’t bother to evaluate the right side of the OR expression if the left side is true.

It’s fine to use the bitwise operators on boolean values; the operators simply treat boolean values as single bits, with true treated as a one and false as a zero. Because these are not short-circuited operations, all operands will be evaluated before the logical operations are applied.

As with other operand evaluation in Java, the operands are evaluated from left to right. Because there are side effects in these evaluations—specifically the printing of messages—it is clear that the output bad funny scary lucky will be printed first. Thus, you can see that the correct option will be either B or C.

Next, you need to determine what the output will be, and for this the precedence of the operations comes into play. The precedence of these three operations, from highest to lowest, is

&
^
|

The expression starts out as follows:

bad() | funny() & scary() ^ lucky()

When the method calls are evaluated, it becomes the following:

false | false & true ^ true

If you add explicit parentheses to illustrate the order of execution, it will be as follows:

(false | ((false & true) ^ true))

Evaluate each subexpression in the required order and substitute the result into the expression. Put the substituted result in bold so it’s easier to see what happened at each step.

First step: false & true evaluates to false, yielding the following substitution:

(false | (false ^ true))

Second step: false ^ true gives true, producing the following substitution:

(false | true)

Third step: false | true gives true and that is the final result of the expression. From this you can see that the output will be bad funny scary lucky true and option B is correct, while options A, C, D, and E are incorrect.

Conclusion. The correct answer is option B.

Related quizzes

Mikalai Zaikin

Mikalai Zaikin is a lead Java developer at IBA Lithuania (part of worldwide IBA Group) and currently located in Vilnius. During his career, Zaikin has helped Oracle with development of Java certification exams, and he has been a technical reviewer of several Java certification books, including three editions of the famous Sun Certified Programmer for Java study guides by Kathy Sierra and Bert Bates.

Simon Roberts

Simon Roberts joined Sun Microsystems in time to teach Sun’s first Java classes in the UK. He created the Sun Certified Java Programmer and Sun Certified Java Developer exams. He wrote several Java certification guides and is currently a freelance educator who publishes recorded and live video training through Pearson InformIT (available direct and through the O’Reilly Safari Books Online service). He remains involved with Oracle’s Java certification projects.


Previous Post

Perform textual sentiment analysis in Java using a deep learning model

Yuli Vasiliev | 9 min read

Next Post


Quiz yourself: The plus + and equals-equals == operators in Java

Simon Roberts | 4 min read