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
Question
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
Solution
The output of the given Java program will be 3.
Here's the step by step explanation:
-
Two integer variables var1 and var2 are declared and initialized with values 3 and 8 respectively.
-
In the if condition, var2 is re-assigned with the value 2.
-
Then, it checks if var2 (which is now 2) is equal to var1 (which is 3). This condition is false.
-
Since the condition in the if statement is false, it goes to the else block.
-
In the else block, it prints the value of ++var2. The '++' before var2 means it's a pre-increment operation, so var2 is incremented by 1 before it's printed.
-
Therefore, var2 becomes 3 and that's what gets printed.
So, the output is 3.
Similar Questions
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 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
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
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 program?class string_class {public static void main(String args[]){String obj = "hello";String obj1 = "world"; String obj2 = obj;obj2 = " world";System.out.println(obj + " " + obj2); }}
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.