What is the correct answer for following conditional operations? int a, b; a = 10; b = (a == 1) ? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10) ? 20: 30; System.out.println( "Value of b is : " + b );
Question
What is the correct answer for following conditional operations? int a, b; a = 10; b = (a == 1) ? 20: 30; System.out.println( "Value of b is : " + b ); b = (a == 10) ? 20: 30; System.out.println( "Value of b is : " + b );
Solution
The code you provided is written in Java and it uses the ternary operator, which is a shorthand for the if-else statement.
Here's how it works:
-
int a, b;- This line declares two integer variables,aandb. -
a = 10;- This line assigns the value10toa. -
b = (a == 1) ? 20: 30;- This line is a ternary operation. It checks ifais equal to1. Ifais equal to1, it assigns the value20tob. Ifais not equal to1, it assigns the value30tob. In this case, sinceais10and not1,bwill be assigned the value30. -
System.out.println( "Value of b is : " + b );- This line prints the value ofb, which is30. -
b = (a == 10) ? 20: 30;- This line is another ternary operation. It checks ifais equal to10. Ifais equal to10, it assigns the value20tob. Ifais not equal to10, it assigns the value30tob. In this case, sinceais10,bwill be assigned the value20. -
System.out.println( "Value of b is : " + b );- This line prints the new value ofb, which is20.
So, the output of the code will be:
Value of b is : 30
Value of b is : 20
Similar Questions
What is the correct answer for following assignment operations? int a = 10; int c = 15; c %= a ; System.out.println("c %= a = " + c ); c <<= 2 ; System.out.println("c <<= 2 = " + c ); c >>= 2 ; System.out.println("c >>= 2 = " + c ); c >>= 2 ; System.out.println("c >>= 2 = " + c );
What will be the output of the following code?int x = 10;if (x > 5) { System.out.println("x is greater than 5");} else { System.out.println("x is less than or equal to 5");}x is greater than 5x is less than or equal to 5x is 10x is less than 10
What would be the output of the following code snippet if variable p=10?if(p<=0){ if(p==0) { System.out.println("4 "); } else { System.out.println("5 "); }}System.out.println("6 ");Options2 31 21 36
What will be result of the following statement boolean a= true; boolean b = false;System.out.println( a || b);
What is the purpose of the "==" operator in Java?Question 20Answera.Assign a value to a variableb.Perform arithmetic additionc.Perform logical OR operationd.Compare two values for equality
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.