LearnFrenzy
Sign in Take a free test

Control Flow Statements · Predict the Output · Question 7

do-while with a false condition Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 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 = 10, runsWhile = 0, runsDo = 0;
        while (i < 5) { runsWhile++; i++; }
        do { runsDo++; i++; } while (i < 5);
        System.out.println(runsWhile + " " + runsDo + " " + i);
    }
}
  • A 0 1 11 Correct
  • B 0 0 10
  • C 1 1 11
  • D 0 1 10
Correct Answer: Option A

The while body never runs; the do-while body runs once before its condition is checked.

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