A.5 Chapter 5: Flow control

Chapter 5 includes four Twist in the Tale exercises.

A.5.1 Twist in the Tale 5.1

Purpose: To emphasize multiple points:

  • A variable of any type can be (re)assigned a value in an expression used in an if condition.
  • if-else-if statements execute each if condition as control is passed to them, changing the value of any variable that’s manipulated in the evaluation of the expression.
  • An expression used in an if condition should evaluate to a boolean value.

Answer: f

Explanation: The flow of execution of code statements in this exercise is shown in figure A.4.

Figure A.4. Flow of execution of code in Twist in the Tale 5.1

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:

  • The initial value of variable score is 10. The first condition ((score = score + 10) == 100) reassigns the value of variable score to 20 and then compares it to the literal integer value 100. The expression 20 == 100 returns a boolean value false. The control doesn’t evaluate the then part of the if construct and moves on to the evaluation of the second if condition defined in the else part.
  • The second condition ((score = score + 29) == 50) adds 29 to the existing value 20 of variable score and then compares the new value 49 with 50. The expression 49 == 50 returns false again. The control doesn’t evaluate the then part of the if construct and moves on to evaluation of the second if condition defined in the else part.
  • The third condition ((score = score + 200) == 10) adds a value of 200 to the existing value 49 of variable score, making it 249, and compares that with the integer literal value 10. Because 249 == 10 evaluates to false, control moves to the else part. The else part assigns a literal value F to the variable result. At the end of execution of the if-else-if statement, the variable score is assigned a value of 249 and result is assigned a value of F. The code outputs F:249.
Exam Tip

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 ++).

A.5.2 Twist in the Tale 5.2

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.

A.5.3 Twist in the Tale 5.3

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.

A.5.4 Twist in the Tale 5.4

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:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.56.12