Question 11
Back to Exercise
What is the output of the following Java program?
public class Main {
public static void main(String[] args) {
int i;
int total = 0;
for (i = 0; i < 5; i++);
{
total += i;
}
System.out.println(i + " " + total);
}
}
- A 5 5 Correct
- B 5 10
- C 4 10
- D 5 0
Correct Answer: Option A
The semicolon is the loop body, so the loop just counts i to 5. The block after it runs once, adding 5.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!