Java Magazine, Nov/Dec 2017
ORACLE COM JAVAMAGAZINE NOVEMBER DECEMBER 2017 97 fix this value isnt too large for a float but it will likely lose precision and thats suficient reason for the compiler to object In this case you can add the F sufix to the literal ensuring that the literal expression is of type float from the beginning But unlike with the long value you can successfully add a cast in front of this one 16 float pi float 314 You might think this inconsistent given that line 14 could not be fixed by adding a cast The difference is that in line 14 the literal expression was not valid so an attempt to cast it would be too late to fix it However in line 16 the literal is valid so a cast can be applied to it and in so doing you overcome the problem of assigning the value to the float sized variable Its probably fair to suggest that the F suffix making the literal a float type expression is the preferred style Line 17 compiles successfully As I mentioned earlier assigning a constant expression to a variable thats large enough for the actual value being assigned is permitted even if the size of the type of the constant expression which is int is too large for the type of the variable being assigned Given this you can see that only line 17 compiles and option D is the correct answer Question 3 The correct answers are options C and E This question investigates the rules related to mutation of collections particularly during iteration In Java 8 there are several ways to iterate over a list These include using the enhanced for loop the forEach method the ListIterator and the simple Iterator thats available on any Iterable and a stream As a general rule any of these built in iteration mechanisms carry warnings that modifying the data structure during the iteration is unsafe and likely but not guaranteed to result in a ConcurrentModificationException being thrown One safe approach could be to use an external iteration such as the one shown in option D However the one shown in option D is fatally flawed because it iterates through to a limiting index the last variable which is extracted at the start of the iteration Therefore if any items are removed this code is guaranteed to iterate beyond the end of the list and so option D is incorrect
You must have JavaScript enabled to view digital editions.