Wednesday, November 23, 2022

Quiz yourself: If/else statements, boolean operations, and side effects

Quiz yourself, Oracle Java Career, Java Skills, Java Jobs, Java Prep, Java Preparation, Java Tutorial and Materials, Java Guides, Java Guides


Given the NineOneOne class

public class NineOneOne {
    static boolean emergency;
    public static void main(String[] args) {
        if (!emergency());
            say1(); // line n1
            if (emergency());
        else if(emergency())
            say2();
    }
    static boolean emergency() {
        return emergency ^= true;
    }
    static void say1() {
        System.out.print("say1 ");
    }
    static void say2() {
        System.out.print("say2 ");
    }
}

What is the outcome if you try to compile and run the code? Choose one.

A. There is a compilation error due to unreachable code at line n1.
B. Compilation succeeds and there is no output.
C. say1
D. say2
E. say1 say2

Answer. This question addresses several aspects. The primary one is perhaps attention to detail, which of course can be a particularly critical skill in debugging code that you did not write. Additional aspects include boolean operations, side effects, and the if/else construction.

The “attention to detail” aspect of this question is rather extreme, and it’s not common to find this level of trickiness in exam questions. Did you notice the semicolons at the end of both lines that start with if? Another matter of detail, albeit related to the first, is that the indentation is inappropriate (and it’s not our intention to turn you into a Python fan!). However, from a syntactic perspective, the code is valid. Furthermore, there is no unreachable code. This tells you that option A is incorrect; the code will compile.

As a side note, unreachable code is typically not considered to be a compilation error in situations where entire blocks of code subordinate to an if or a case are unreachable. These special cases are intended to permit the equivalent of conditional compilation and can be useful in situations such as debugging and testing because they allow entire chunks of code to be enabled, disabled, or swapped out. Therefore, the following does not elicit any complaint from the compiler:

if (false) {
  System.out.println("Debugging");
  // more code
}

By contrast, swapping the if for a while, as in the following code, is rejected by the compiler as unreachable:

while (false) { // error: unreachable statement
  System.out.println("Debugging");
  // more code
}

Moving to the other options: You should analyze the Boolean variable called emergency. The variable is not initialized explicitly, but it’s an instance field, so it is reliably initialized to false during the object’s allocation phase.

Quiz yourself, Oracle Java Career, Java Skills, Java Jobs, Java Prep, Java Preparation, Java Tutorial and Materials, Java Guides, Java Guides
Notice that the emergency() method inverts the value of the emergency field each time it is called. This is performed by the exclusive OR (XOR) assignment operator (^=), and the code returns the resulting value each time it is called.

Now that you know what’s going on with the emergency field and method, look at the body of the code that calls that method. The first two if tests do nothing conditional, because they end in semicolons. However, they serve to invert (to true) and then reinvert (back to false) the value of the emergency variable.

The indentation of the code is misleading, but once you’ve spotted the semicolons at the end of those first two if statements, you’ll recognize that the method say1() will be invoked no matter what value the emergency() method returns.

At the second if statement, the value returned from the emergency() method is false, so control goes to the else branch and the third if—and with it, the third call to emergency(). That third call returns true, which invokes the say2() method.

Because both the say1() and say2() methods are invoked, say1 say2 is printed to the console. This means option E is correct and options A, B, C, and D are incorrect.

Conclusion. The correct answer is option E.

Source: oracle.com

Related Posts

0 comments:

Post a Comment