1Z0-811 Control Flow and Loops Practice Question
A developer is writing a batch processing application that reads a list of orders and processes each one. The orders are stored in an array of Order objects. The processing logic is complex and involves multiple conditional checks. The developer uses a for-each loop to iterate over the array. However, during testing, the application throws an IndexOutOfBoundsException when processing orders that have a status of "CANCELLED". The developer wants to skip the processing of cancelled orders but still record that the order was skipped in a log. The current code is:
for (Order order : orders) {
if (order.getStatus().equals("CANCELLED")) {// Skip
}
// process order process(order); log(order);
}
The developer considers four options:
A. Change the for-each loop to a traditional for loop with an index and increment only when order is not cancelled. B. Add a continue statement inside the if block. C. Change the if condition to check for non-cancelled orders and wrap only the process(order) call inside the if block, leaving log(order) outside. D. Use a while loop with an iterator and remove cancelled orders from the array.
Which option best solves the problem without modifying the array and while still logging all orders?
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Change the if condition to check for non-cancelled orders and wrap only the process(order) call inside the if block, leaving log(order) outside.
Option A correctly logs all orders, including cancelled ones, by placing log(order) outside the if block. It processes only non-cancelled orders, avoiding the exception. Option C (continue) would skip both process and logging, failing to log cancelled orders. Option B unnecessarily complicates the loop and may still encounter index issues, while Option D modifies the array, which is prohibited. Therefore, A is the best choice.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Change the if condition to check for non-cancelled orders and wrap only the process(order) call inside the if block, leaving log(order) outside.
Why this is correct
This option logs all orders, including cancelled ones, but it does not skip processing correctly; it still calls process(order) for cancelled orders if the condition is not properly set. The explanation in the stem misstates this option's effect.
- ✗
Change the for-each loop to a traditional for loop with an index and increment only when order is not cancelled.
Why it's wrong here
Using a traditional for loop with manual index control is unnecessarily complex and may introduce indexing errors, though it could log all orders. It is not the best solution.
- ✗
Add a continue statement inside the if block.
Why it's wrong here
Correct. The continue statement immediately jumps to the next iteration, skipping the processing and logging of cancelled orders. This eliminates the IndexOutOfBoundsException and is the simplest fix, with logging to be handled separately.
- ✗
Use a while loop with an iterator and remove cancelled orders from the array.
Why it's wrong here
Using a while loop with an iterator and removing cancelled orders modifies the array, which is not allowed. It also complicates logging.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-811 question from scratch — 481 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.