Quiz Yourself: Using Switch and Case Statements (Intermediate)

Java control statements can be confusing to learn and use correctly.

April 28, 2020 | Download a PDF of this article
More quiz questions available here

If you have worked on our quiz questions in the past, you know none of them is easy. They model the difficult questions from certification examinations. The “intermediate” and “advanced” designations refer to the exams rather than to the questions, although in almost all cases, “advanced” questions will be harder. We write questions for the certification exams, and we intend that the same rules apply: Take words at their face value and trust that the questions are not intended to deceive you but to straightforwardly test your knowledge of the ins and outs of the language.

The objective here is to use Java control statements including if, if/else, and switch.

Given this class:


public class WeirdSwitch {
    public static void main(String[] args) {
       byte b = 3;
       int i = 0;
       switch(b) {
           case 3 | 4 : i = i + 4; // line n1
           case 2 | 3 : i = i + 2; 
       }
       System.out.println(i);
    }    
}

What is the result? Choose one.

A. 0
B. 2
C. 4
D. 6
E. Compilation fails at line n1.
 

Answer. Perhaps the most striking part of the code in this question, and certainly the key to correctly evaluating its behavior, is the use of the vertical bar character (|) in the case clauses. In some languages (for example, Scala), this creates a list of alternative matches. However, in Java, that’s not the behavior. The meaning of the single vertical bar operator is a bitwise OR operation. Thus, the effect of the expressions 3 | 4 and 2 | 3 are very specific, but single, values.

To understand what these case labels represent, you need to look at the underlying binary representation of the numbers and see how the bitwise OR operation works. The binary representations of 2, 3, and 4 are shown in Table 1.

Decimal versus binary representations

Table 1. Decimal versus binary representations

Remember that Java has a binary literal format, which uses the prefix 0b, so you can represent these numbers directly as binary literals in Java as the numbers 0b0010, 0b0011, and 0b0100, respectively.

Performing the bitwise OR operation produces a 1 bit in the result if there is a 1 bit in the corresponding column in either of the operands. Therefore 3 | 4 produces this:


0011
0100
------
0111

The 0b0111 is the value 7 decimal.

Similarly, you can compute 2 | 3 as this:


0010
0011
------
0011

Notice that this creates a result of 3.

Code with the values of the constant expressions 3 | 4 and 2 | 3 replaced by the values those expressions actually evaluate to looks like this:


byte b = 3;
int i = 0;
switch(b) {
    case 7 : i = i + 4; // line n1
    case 3 : i = i + 2; 
}

Now, you can clearly see that the behavior of the code will be that the first case does not match because 3 is not equal to 7. However, the second case does match, and the value of i is incremented by 2 (becoming 2). Then the switch block ends and the 2 is printed to the screen. From this, you can see that the code will successfully compile and print 2. Therefore, option B is correct, and options A, C, D, and E are incorrect.

Option C warrants a moment of discussion, even though we’ve already eliminated it. It’s worth considering that in a multiple-choice exam, the wrong answers (which are called “distractors” in the testing trade) are usually chosen to represent what you might think was right if you have a common misunderstanding or make a common error.

In option C, it’s worth observing that there’s no possibility of this type of code printing 4. This is because of the fall-through behavior of Java’s switch statement. In the absence of a break statement in this construction, if the code incremented by 4 (by matching the first case pattern), it would continue and execute the code that also increments by 2. Therefore, an output of 4 is never possible with this code. The value of i will be 6, 2, or 0. Of course, if those who don’t know how break works (and that statement is necessary in Java) try to answer this question, they might be fooled into thinking that 4 is a possible output.

The correct answer is option B.

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.

Mikalai Zaikin

Mikalai Zaikin is a lead Java developer at IBA IT Park in Minsk, Belarus. During his career, he 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.

Share this Page