Knowee
Questions
Features
Study Tools

What is the output of this program? class A {public static void main(String args[]){ int var1 = 5; int var2 = 6; if ((var2 = 1) == var1)System.out.print(var2);else System.out.print(++var2); }}ans. 2314 Previous Marked for Review Next

Question

What is the output of this program? class A {public static void main(String args[]){ int var1 = 5; int var2 = 6; if ((var2 = 1) == var1)System.out.print(var2);else System.out.print(++var2); }}ans. 2314 Previous Marked for Review Next

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

Solution

The output of the program will be 2.

Here's the step by step explanation:

  1. The program starts with the declaration of class A and the main method.

  2. Two integer variables, var1 and var2, are declared and initialized with the values 5 and 6 respectively.

  3. Then there is an if statement. In the condition of the if statement, var2 is assigned the value 1. This assignment operation also returns the value that was assigned, which is 1.

  4. This returned value (1) is then compared with var1 (5). Since 1 is not equal to 5, the condition of the if statement is false.

  5. Therefore, the program skips the first print statement and goes to the else part.

  6. In the else part, var2 is incremented by 1 (because of the ++ operator before var2), making var2 equal to 2.

  7. This incremented value of var2 (which is 2) is then printed.

So, the output of the program is 2.

This problem has been solved

Similar Questions

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 is the output of the below program?public class Main{ public static void main(String[] args) { int a = 25; if(a > 5) System.out.print("Hi "); if(a < 20) System.out.print("Hello "); else System.out.print("Know Program "); }}

What is the output of the following code :class MyClass {public static void main(String[] args) {int i=1,j=1;for (;i<3;i++){for(;j<4;j++);j++;System.out.print(i + j+" ");}}

e correct answerWhat is the output of the following Java program?class Main { public static void main(String args[]) { final int i; i = 20; i = 30; System.out.println(i); }}

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

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.