A programmer wants to iterate over a list of strings and print each that starts with 'A'. Which loop construct is best suited?
Simplest for iterating over all elements.
Why this answer
The enhanced for loop (for-each) is best suited because it provides a clean, concise syntax for iterating over a collection like a List<String> without needing an explicit index or iterator. It directly yields each element, allowing a simple if-statement to check if the string starts with 'A' and print it, making the code more readable and less error-prone.
Exam trap
The 1Z0-811 exam often tests the misconception that a traditional for loop with an index is always the most flexible or efficient choice, but for simple element access without index manipulation, the enhanced for loop is the idiomatic and recommended construct in Java.
How to eliminate wrong answers
Option A is wrong because a do-while loop is a post-test loop that always executes the body at least once, which is unnecessary and less readable for iterating over a list where the number of elements is known. Option C is wrong because a traditional for loop with an index requires manual index management and bounds checking, adding complexity and potential off-by-one errors without any benefit for simple element access. Option D is wrong because a while loop with an iterator, while functional, requires explicit calls to hasNext() and next(), introducing more boilerplate and the risk of NoSuchElementException if not handled correctly, making it less elegant than the enhanced for loop.