Knowee
Questions
Features
Study Tools

What will be the output of the following Java program?class conditional_statements {    public static void main(String args[])     {         final int u=10,v=20;     while(u<v)     {      System.out.println("Good");     }     System.out.println("Morning");     } }Optionsrun time errorGoodGood Morningcompile time error

Question

What will be the output of the following Java program?class conditional_statements {    public static void main(String args[])     {         final int u=10,v=20;     while(u<v)     {      System.out.println("Good");     }     System.out.println("Morning");     } }Optionsrun time errorGoodGood Morningcompile time error

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

Solution

The output of the given Java program will be a run time error.

Here's why:

The condition in the while loop u<v is always true because the values of u and v are final (constant) and u is less than v. This results in an infinite loop where "Good" is printed out indefinitely.

The program does not reach the line System.out.println("Morning"); because it is stuck in the infinite loop.

However, the Java compiler does not detect this infinite loop, so there is no compile time error. The error occurs at run time when the program does not terminate as expected.

This problem has been solved

Similar Questions

rect answerWhat will be the output of the following Java code? class Codetantra {    public static void main(String args[]) {         try {             System.out.print("Good" + " " + 1 / 0);          }         finally {        System.out.print("Morning");                }     } }

What will be the output of the following Java program?class mainclass { public static void main(String args[]) { boolean ct1 = true; boolean ct2 = false; if (ct1) System.out.println(ct1); else System.out.println(ct2); } }Options10truefalse

What will be the output of the following Java program?class java   {    public static void main(String args[])    {      int var1 = 3;       int var2 = 8;      if ((var2 = 2) == var1)        System.out.print(var2);      else         System.out.print(++var2);    }   }Options3421

What will be the output of the following Java program?class java  {    public static void main(String args[])     {          int w = 5;       int x = 10;       first:        {        second:         {          third:           {             if (w == x >> 1)              break second;          }          System.out.println(w);        }        System.out.println(x);       }    }   }Options10 55 10510

What will be the output of the following Java program?class control_statements  {    public static void main(String args[])     {          int sum = 0;       for (int u = 5, v = 5; u < 10 & v < 10; ++u, v = u + 1)         sum += u;     System.out.println(sum);    }   }Options261015compilation error

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.