Iteration in Programming
What is the primary purpose of iteration in programming?
To define variables.
To execute a block of code repeatedly.
To declare classes.
To create objects.
What will be the output of the following code snippet?
java
int i = 0;
while (i < 3) {
System.out.print(i);
i++;
}
0123
123
012
Error
What will be the value of x
after the following code is executed?
java
int x = 0;
int i = 5;
while (i > 0) {
x += i;
i--;
}
0
5
15
20
Consider the following code:
java
int i = 0;
while (i < 5) {
if (i % 2 == 0) {
System.out.print(i + " ");
}
}
What is the most significant problem with this loop?
The loop will not execute at all.
The loop will execute only once.
The loop will result in a compilation error.
The loop will run infinitely.
How many times will the following for loop execute?
java
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
9
10
11
0
What will be the output of the following code snippet?
java
int sum = 0;
for (int i = 1; i <= 4; i++) {
sum += i;
}
System.out.println(sum);
4
6
10
15
What is the final value of j
after the following code executes?
java
int j = 0;
for (int i = 0; i < 5; i += 2) {
j += i;
}
0
2
4
6

How are we doing?
Give us your feedback and let us know how we can improve
Given the string String str = "hello";
, what is the value of str.length()
?
4
5
6
0
What is the purpose of the following code snippet?
java
String str = "programming";
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i + 1).equals("m")) {
count++;
}
}
Reverse the string.
Count the number of vowels in the string.
Count the number of 'm' characters in the string.
Find a substring within the string.
What code will reverse the string hello
?
java
String str = "hello";
String reversed = "";
for (int i = 0; i < str.length(); i++) {
reversed = str.charAt(i) + reversed;
}
java
String str = "hello";
String reversed = "";
for (int i = 0; i < str.length(); i++) {
reversed += str.charAt(i);
}
java
String str = "hello";
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--) {
reversed += str.substring(i, i + 1);
}
java
String str = "hello";
String reversed = "";
for (int i = str.length(); i >= 0; i--) {
reversed += str.charAt(i);
}