Knowee
Questions
Features
Study Tools

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 );

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

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:

  1. int a, b; - This line declares two integer variables, a and b.

  2. a = 10; - This line assigns the value 10 to a.

  3. b = (a == 1) ? 20: 30; - This line is a ternary operation. It checks if a is equal to 1. If a is equal to 1, it assigns the value 20 to b. If a is not equal to 1, it assigns the value 30 to b. In this case, since a is 10 and not 1, b will be assigned the value 30.

  4. System.out.println( "Value of b is : " + b ); - This line prints the value of b, which is 30.

  5. b = (a == 10) ? 20: 30; - This line is another ternary operation. It checks if a is equal to 10. If a is equal to 10, it assigns the value 20 to b. If a is not equal to 10, it assigns the value 30 to b. In this case, since a is 10, b will be assigned the value 20.

  6. System.out.println( "Value of b is : " + b ); - This line prints the new value of b, which is 20.

So, the output of the code will be:

Value of b is : 30
Value of b is : 20

This problem has been solved

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

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.