Iteration Quiz Latest Update Already
Passed
What does a for loop in Java typically require?
A) Initialization, condition, and iteration step
B) Only the condition
C) Initialization and iteration
D) Condition and iteration step
✔✔ A) Initialization, condition, and iteration step
Which of the following is a valid loop structure in Java?
A) for (int i = 0; i < 10)
B) for (int i = 0; i < 10; i++)
C) for i = 0 to 10
D) for (int i; i < 10)
✔✔ B) for (int i = 0; i < 10; i++)
In a while loop, when does the loop body execute?
1
,A) At the end of the loop only
B) As long as the condition evaluates to true
C) When the condition is false
D) The loop does not have a body
✔✔ B) As long as the condition evaluates to true
Which of the following is the proper syntax to declare a for loop?
A) for int i = 0 to 10
B) for (int i = 0; i < 10)
C) for (int i = 0; i < 10; i++)
D) for (int i; i < 10)
✔✔ C) for (int i = 0; i < 10; i++)
What does the statement "i++" do inside a loop?
A) Increments the value of i by 1
B) Sets the value of i to 1
C) Decrements the value of i by 1
D) Sets the value of i to 0
2
, ✔✔ A) Increments the value of i by 1
What will be the output of the following code snippet?
```java
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += i;
}
System.out.println(sum);
```
A) 5
B) 0
C) 10
D) 15
✔✔ C) 10
What will happen if the condition in a while loop is never met?
3