LearnFrenzy
Sign in Take a free test

Control Flow Statements · Predict the Output · Question 5

How many times a loop with a custom step runs Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 5 Back to Exercise

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        int a = 0, b = 0, c = 0;
        for (int i = 0; i < 20; i += 3) a++;
        for (int i = 20; i > 0; i /= 2) b++;
        for (int i = 1; i <= 1000; i *= 10) c++;
        System.out.println(a + " " + b + " " + c);
    }
}
  • A 7 5 4 Correct
  • B 6 5 4
  • C 7 4 3
  • D 7 5 3
Correct Answer: Option A

0, 3, … 18 is 7 values; 20, 10, 5, 2, 1 is 5; 1, 10, 100, 1000 is 4.

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