Java_Magazine_NovemberDecember_2018

(singke) #1

80


// fi x t h i s /


Question 4 (intermediate). Given the following code fragment:


String[] nums = {"One", "Two", "Three", "Four", "Five", "Six", "Sev"};
for(int i = 0; i < nums.length; i++) {
if (nums[i++].length() % 3 == 0) {
continue;
}
System.out.println(nums[i]);
break;
}

What is the output?
A. Three
B. Four
C. Five
D. Three
Four
Five
E. No output

Answer 1. Option C is correct. The enhanced for expression was introduced in Java SE 5, and it
provides a higher level of abstraction iterating over the elements of an Iterable (for example, a
Set or a List) or an array.
Prior to the enhanced for loop, developers would describe how to iterate—for example, by
declaring a loop and a counter variable, incrementing the counter, and comparing it against the

Answer 4
page 88

Answers


Question 1
page 78
Free download pdf