Iteration constructs in Java Iteration constructs in Java Basic for-loop, Nested Loops, Infinite Loop, Enhanced for-loop, Break Statement, Continue Statement, Do-While Loop, Nested Loops with Break, For-loop with Multiple Variables, Infinite While Loop 1 / 10 What will the following code output? for (int i = 0; i < 5; i++) { System.out.print(i + " "); } 1 2 3 4 5 0 1 2 3 4 0 1 2 3 4 5 Compilation Error The loop starts at 0 and runs until i is less than 5. The value of i is printed in each iteration. The loop starts at 0 and runs until i is less than 5. The value of i is printed in each iteration. 2 / 10 What will the following code output? for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 2; j++) { System.out.print(i * j + " "); } } 1 2 2 4 3 6 1 2 3 4 5 6 2 4 6 8 Compilation Error The outer loop runs 3 times, and the inner loop runs 2 times per iteration. The product of i and j is printed in each inner loop. The outer loop runs 3 times, and the inner loop runs 2 times per iteration. The product of i and j is printed in each inner loop. 3 / 10 Which of the following results in an infinite loop? for (int i = 0; i < 5; i–) { System.out.println(i); } while (true) { System.out.println("Hello"); } do { System.out.println("Looping"); } while (1 > 0); All of the above Each example contains a condition or logic that leads to an infinite loop. Each example contains a condition or logic that leads to an infinite loop. 4 / 10 What will the following code output? int[] numbers = {1, 2, 3, 4, 5}; for (int num : numbers) { System.out.print(num + ” “); } 1 2 3 4 5 5 4 3 2 1 1 3 5 Compilation Error The enhanced for-loop iterates through the array numbers in the order of its elements. The enhanced for-loop iterates through the array numbers in the order of its elements. 5 / 10 What will the following code output? for (int i = 1; i <= 5; i++) { if (i == 3) break; System.out.print(i + " "); } 1 2 3 1 2 1 2 3 4 5 Compilation Error 6 / 10 What will the following code output? for (int i = 1; i <= 5; i++) { if (i == 3) continue; System.out.print(i + " "); } 1 2 4 5 1 2 3 4 5 1 2 Compilation Error When i == 3, the continue statement skips the rest of the loop body for that iteration, so 3 is not printed. When i == 3, the continue statement skips the rest of the loop body for that iteration, so 3 is not printed. 7 / 10 What will the following code output? int i = 1; do { System.out.print(i + ” “); i++; } while (i <= 3); 1 1 2 3 4 Compilation Error 1 2 3 A do-while loop executes at least once, then checks the condition at the end of each iteration.) A do-while loop executes at least once, then checks the condition at the end of each iteration.) 8 / 10 What will the following code output? for (int i = 1; i <= 3; i++) { for (int j = 1; j <= 3; j++) { if (i == j) break; System.out.print(i + "" + j + " "); } } 12 13 23 12 13 23 32 21 23 31 32 12 13 21 23 31 32 The inner loop breaks when i == j, skipping those pairs. The inner loop breaks when i == j, skipping those pairs. 9 / 10 What will the following code output? for (int i = 0, j = 5; i < j; i++, j--) { System.out.print(i + "" + j + " "); } 05 14 23 05 14 23 32 05 14 Compilation Error The loop uses two variables, incrementing i and decrementing j. The condition i < j ensures the loop runs until i and j meet. The loop uses two variables, incrementing i and decrementing j. The condition i < j ensures the loop runs until i and j meet. 10 / 10 Which of the following will result in an infinite loop? int i = 0; while (i < 5) { System.out.println(i); } int i = 0; while (true) { System.out.println(i); i++; } int i = 0; while (i < 5) { System.out.println(i); i = 0; } All of the above a) lacks an increment for i, b) has an always-true condition, and c) resets i to 0 in each iteration, all causing infinite loops. a) lacks an increment for i, b) has an always-true condition, and c) resets i to 0 in each iteration, all causing infinite loops. Your score is By WordPress Quiz plugin