Home / Questions / What are the values of x and y when the following statements are executed? int a = 63, b = 36; boolean x = (a > b)? true : false; int y = (a < b)? a : b;
Explanatory Question

What are the values of x and y when the following statements are executed?

int a = 63, b = 36;
boolean x = (a > b)? true : false;
int y = (a < b)? a : b;

👁 90 Views
📘 Detailed Answer
🕒 Easy to Read
Read the answer carefully and go through the related questions on the right side to improve your understanding of this topic.

Answer with Explanation

Let’s analyze the given statements step by step:

  1. Boolean Expression for x:


boolean x = (a > b) ? true : false;

  • Here, the ternary operator (condition) ? true : false evaluates the condition a > b and assigns true if the condition is true; otherwise, it assigns false.

    • Condition: a > b
    • Values: a = 63 and b = 36
    • Evaluation: 63 > 36 is true.

    Thus, x will be assigned true.

  • Integer Expression for y:


int y = (a < b) ? a : b;

  1. Similarly, the ternary operator (condition) ? a : b evaluates the condition a < b and assigns a if the condition is true; otherwise, it assigns b.

    • Condition: a < b
    • Values: a = 63 and b = 36
    • Evaluation: 63 < 36 is false.

    Thus, y will be assigned b, which is 36.

Summary:

  • The value of x is true.
  • The value of y is 36.