1Z0-811 Control Flow and Loops • Complete Question Bank
Complete 1Z0-811 Control Flow and Loops question bank — all 0 questions with answers and detailed explanations.
What is the output of the following code?
int i = 0;
while (i < 5) {
if (i == 3) {i++; continue;
}
System.out.print(i + " "); i++;
}
Refer to the exhibit.
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
System.out.print(i + " ");
}Refer to the exhibit.
int sum = 0;
for (int i = 1; i <= 10; i++) {
if (i % 3 == 0) {
break;
}
sum += i;
}
System.out.println(sum);Drag steps to the numbered slots on the right, or tap a step then tap a slot.
Drag a concept onto its matching description — or click a concept then click the description.
Increment by 1
Modulo (remainder) operator
Logical AND (short-circuit)
Checks if an object is of a certain type
Ternary conditional operator
Refer to the exhibit.
public class LoopTest {
public static void main(String[] args) {
int count = 0;
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
count++;
}
System.out.println(count);
}
}Refer to the exhibit.
public class SwitchTest {
public static void main(String[] args) {
int x = 2;
switch(x) {
case 1:
System.out.print("One ");
case 2:
System.out.print("Two ");
case 3:
System.out.print("Three ");
break;
default:
System.out.print("Default");
}
}
}Refer to the exhibit.
public class NestedLoop {
public static void main(String[] args) {
outer:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {
break outer;
}
System.out.println(i + "," + j);
}
}
}
}int[] data = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i <= data.length; i++) {
sum += data[i];
}
System.out.println(sum);A developer implements a menu-driven application using a switch statement inside a do-while loop. The menu options are 1-4, and option 4 is supposed to exit the application. However, after selecting option 4, the menu is displayed again before the application exits. The code is structured as follows: do { displayMenu(); choice = scanner.nextInt(); switch(choice) { case 1: // process option 1 break; case 2: // process option 2 break; case 3: // process option 3 break; case 4: break; // intended to exit loop
} } while(choice != 4);
The developer realizes that the break inside the switch only exits the switch, not the loop. What is the correct fix?
int count = 0;
for (int i = 0; i < 3; i++) {
count++;
if (i == 1) continue;
count++;
}
System.out.println(count);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?