Chapter 5 includes four Twist in the Tale exercises.
Purpose: To emphasize multiple points:
Answer: f
Explanation: The flow of execution of code statements in this exercise is shown in figure A.4.
The arrows on the left in figure A.4 show the flow of execution of statements for this code snippet. The if conditions on the right show the actual values that are compared after the expression used in the if statements is evaluated. Following is a detailed description:
This exercise is a nice opportunity to remind you that such assignations are always performed before the test or other expression they are part of (that is, preassignations) except for post-incrementations (that is, postfixed ++).
Purpose: The switch construct uses the equals method to compare the value of its argument with the case values. It doesn’t compare the variable references.
Answer: c
Explanation: You may have answered questions with code like the following, which prints false:
String aDay = new String("SUN"); System.out.println(aDay == "SUN");
String objects that are created using the assignment operator (=) are stored in a pool of String objects, but String objects that are created using the operator new aren’t stored in the pool of String objects.
When a String object is passed as an argument to a switch construct, it doesn’t compare the object references; it compares the object values using the equals method. In the code snippet shown in the question, a match is found for the String literal value SUN, so the code prints Weekend!, executes the break statement, and exits the block.
Purpose: Note the type of the variable that’s passed as an argument to the switch construct. Among the primitive data types, you can pass on variables of types byte, short, char, and int to a switch construct. Other data types that you can pass to a switch construct are Byte, Short, Integer, Character, enum, and String.
This question tries to take your attention off this simple basic requirement and to move your focus to the logic of the question.
Answer: The submission by Harry.
Explanation: Paul’s submission doesn’t compile because a switch construct doesn’t accept an argument of the long primitive data type.
Purpose: When an unlabeled break statement is used within nested loops (for any combinations of for, do-while, or while loops), a break statement will end the execution of the inner loop, not all the nested loops. The outer loop will continue to execute, starting with its next iteration value.
Answer: a
Explanation: Let’s start with the outer loop’s first iteration. In the first iteration, the value of the variable outer is Outer.
For the outer loop’s first iteration, the inner loop should execute for the values Outer and Inner for the variable inner. For the first iteration of the inner loop, the value of the variable inner is Outer, so the condition inner.equals("Inner") evaluates to false and the break statement doesn’t execute. The code prints the value of the variable inner, which is Outer:, and starts with the next iteration of the inner loop. In the second iteration of the inner loop, the value of the variable inner is Inner, so the condition inner.equals("Inner") evaluates to true and the break statement executes, ending the execution of the inner loop and skipping the code that prints out the value of the variable inner.
The outer loop starts its execution with the second iteration. In this iteration, the value of the variable outer is Outer. For the outer loop’s iteration, the inner loop executes twice in the same manner as mentioned in the previous paragraph. This iteration of the outer loop again prints the value of the variable inner when it’s equal to Outer.
The nested loops included in the question print out the value Outer: twice:
Outer:Outer:
52.15.56.12