What all gets printed when the following program is compiled and run.public class Test{ public static void main(String args[]){ int i, j=1; i = (j>1)?2:1; switch(i){ case 0: System.out.println(0); break; case 1: System.out.println(1); case 2: System.out.println(2); break; case 3: System.out.println(3); break; } }}01212
Question
What all gets printed when the following program is compiled and run.public class Test{ public static void main(String args[]){ int i, j=1; i = (j>1)?2:1; switch(i){ case 0: System.out.println(0); break; case 1: System.out.println(1); case 2: System.out.println(2); break; case 3: System.out.println(3); break; } }}01212
Solution
The output of the program will be:
1 2
Here's the step by step explanation:
-
The program starts with the declaration of two integer variables i and j, where j is initialized with the value 1.
-
The next line is a ternary operation which checks if j is greater than 1. If it is, it assigns the value 2 to i, otherwise it assigns the value 1 to i. Since j is 1, which is not greater than 1, the value 1 is assigned to i.
-
The switch statement checks the value of i. Since i is 1, it matches the case 1.
-
The program then executes the statement under case 1, which is System.out.println(1);. This prints the value 1.
-
After printing 1, the program doesn't encounter a break statement, so it continues to the next case, which is case 2.
-
The program then executes the statement under case 2, which is System.out.println(2);. This prints the value 2.
-
After printing 2, the program encounters a break statement, so it exits the switch statement.
-
The program has no more statements to execute, so it ends.
Similar Questions
What all gets printed when the following program is compiled and run?public class Test{ public static void main(String args[]){ int i=0, j=2; do{ i=++i; j--; }while(j>0); System.out.println(i); }}012The program does not compile because of statement "i=++i
What will be the output of the program?public class Switch2 { final static short x = 2; public static int y = 0; public static void main(String [] args) { for (int z=0; z < 4; z++) { switch (z) { case x: System.out.print("0 "); default: System.out.print("def "); case x-1: System.out.print("1 "); break; case x-2: System.out.print("2 "); } } }}0 def 12 1 0 def 12 1 0 def def2 1 0 def 1 def 1
What will be the output of the program?public class Test { public static void main(String args[]) { int i = 1, j = 0; switch(i) { case 2: j += 6; case 4: j += 1; default: j += 2; case 0: j += 4; } System.out.println("j = " + j); } }j = 0j = 2j = 4j = 6
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 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
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.