Iteration in Programming
What is the output of the following code?
int x = 1;
while (x < 5) {
System.out.print(x + " ");
x ++;
}
1 2 3
1 3 5
1 2 3 4
1 2 3 4 5
What does the following code snippet do?
public static int sum() {
int i = 0;
int sum = 0;
while (i <= 5) {
if (i == 2) {
continue;
}
sum += i;
}
return sum;
}
It calculates the sum of all numbers from 0 to 5, excluding the number 2.
It calculates the sum of all numbers from 2 to 5.
It calculates the sum of all even numbers from 0 to 5.
It calculates the sum of all the numbers from 0 to 5.
What is the output of the following code?
int i = 10;
while (i >= 0) {
if (i % 2 == 0) {
System.out.print(i + " ");
}
i--;
}
10 8 6 4 2
9 7 5 3 1
10 8 6 4 2 0
10 9 8 7 6 5 4 3 2 1 0
What is the purpose of the continue statement in a loop?
To skip to the next loop.
To terminate the loop immediately.
To restart the loop from the beginning.
To jump to the next iteration of the loop.
When coding with loops, what is the purpose of using try-catch statements?
To handle exceptions and prevent the program from crashing.
To catch logical errors in the code.
To improve the efficiency of the loop.
To exit the loop when a specific condition is met.
What is the purpose of a while loop?
To exit the program immediately.
To repeat an action while a condition is true.
To repeat a set of actions a fixed number of times.
To execute a set of actions once.
How many times does a loop with a condition that is always false execute its loop body?
It executes the loop body once.
It executes the loop body indefinitely.
It executes the loop body ten times.
It does not execute the loop body at all.

How are we doing?
Give us your feedback and let us know how we can improve
What is the purpose of a sentinel in a loop?
To count the number of iterations in the loop.
To track the current state of the loop.
To handle exceptions thrown by the loop.
To indicate when the loop should stop executing.
Which of the following represents the anatomy of a while
loop?
while {
// Loop body;
}
while {
// Loop body
condition;
}
while (condition) {
// Loop body
}
while (condition):
// Loop body
What happens if the condition of a while loop is always true?
An infinite loop is created.
The loop will exit after one iteration.
The loop will never run.
The loop will skip to the next iteration.