The code compiles and runs without issue
Introduction xxxix
Answers to Assessment Test
■ The loop continues as count loop expression evaluates to 0 < 3, which is true, with y taking a new value of 1. The value of y is set to:
y = (1 + 2 * 1) % 3
x = 3 - 1 = 2
■ The loop continues as count loop expression evaluates to 1 < 3, which is true, with y taking a new value of 2. The value of y is set to:
xl | ■ |
|
---|---|---|
|
= 7 % 3
= 1
6. B. The code compiles successfully, so options D and E are incorrect. The value of a cannot be changed by the addToInt method, no matter what the method does, because only a copy of the variable is passed into the parameter x. Therefore, a does not change and the output on line 9 is 15. For more information, see Chapter 4.
7. B. The array is allowed to use an anonymous initializer because it is in the same line as the declaration. The ArrayList uses the diamond operator allowed since Java 7. This specifies the type matches the one on the left without having to re-type it. After adding the two elements, list contains [6, 8]. We then replace the element at index 1 with 9, resulting in [6, 9]. Finally, we remove the element at index 0, leaving [9]. Option C is incorrect because arrays output something like that rather than an ArrayList. For more information, see Chapter 3.