Knowee
Questions
Features
Study Tools

class Test {    public static void main(String[] args) {        for(int i = 0; 1; i++) {            System.out.println("Hello");            break;        }    }}Compiler ErrorRun Time ErrorHello Hello HelloHello

Question

class Test {    public static void main(String[] args) {        for(int i = 0; 1; i++) {            System.out.println("Hello");            break;        }    }}Compiler ErrorRun Time ErrorHello Hello HelloHello

🧐 Not the exact question you are looking for?Go ask a question

Solution

The code you provided will result in a compiler error. The reason is that the condition in the for loop (1) is not a boolean expression. In Java, the condition in the for loop must be a boolean expression. It should be something like i < 10 or i != 0, etc. Here is the corrected code:

class Test {
    public static void main(String[] args) {
        for(int i = 0; i < 1; i++) {
            System.out.println("Hello");
            break;
        }
    }
}

This will print "Hello" once and then break out of the loop.

This problem has been solved

Similar Questions

class Test {    protected int x, y;} class Main {    public static void main(String args[]) {        Test t = new Test();        System.out.println(t.x + " " + t.y);    }}0 0Run Time ErrorCompiler ErrorNone of the above

public class Test {    public void  m1(String arg1)  {        arg1="Test";    } public static void main(String[] args) {        Test test1= new Test();        String s= "Hello";        test1.m1(s);        System.out.println(s);  }}

Select the correct answerPredict the output of following Java program.class codetantra {   public static void main(String[] args) {    for(int i = 0; 0; i++)    {      System.out.println("Hello");      break;    }   } } OptionsCompiler errorEmpty OutputHelloRuntime error

Select the correct answerPredict the output of following Java program.?class CT {      public static void main(String args[]) {               int a;               System.out.println(a);     }   } Optionscompiler errorgarbage value0runtime error

Cho biết kết quả hiển thị của đoạn mã sau:class TestA{public void start(){System.out.println("TestA");}}class TestB extends TestA{public void start(){System.out.println("TestB");}public static void main(String[] args){((TestA)new TestB()).start();}}doneTestATestBLỗi biên dịchLỗi thực thi

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.