Iteration in Programming
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 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.
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 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
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 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.
What kind of control structure is used to perform iteration?
Conditional statements
Exception handling statements
Branching statements
Loop statements

How are we doing?
Give us your feedback and let us know how we can improve
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.
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.
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