LearnFrenzy
Sign in Take a free test

Control Flow Statements · Predict the Output · Question 11

A for loop with a semicolon after its header Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following Java program?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links