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;
Answer:
Let’s analyze the given statements step by step:
-
Boolean Expression for
x:
boolean x = (a > b) ? true : false;
-
Here, the ternary operator
(condition) ? true : falseevaluates the conditiona > band assignstrueif the condition is true; otherwise, it assignsfalse.- Condition:
a > b - Values:
a = 63andb = 36 - Evaluation:
63 > 36is true.
Thus,
xwill be assignedtrue. - Condition:
-
Integer Expression for
y:
int y = (a < b) ? a : b;
-
Similarly, the ternary operator
(condition) ? a : bevaluates the conditiona < band assignsaif the condition is true; otherwise, it assignsb.- Condition:
a < b - Values:
a = 63andb = 36 - Evaluation:
63 < 36is false.
Thus,
ywill be assignedb, which is36. - Condition:
Summary:
- The value of
xistrue. - The value of
yis36.
Related Articles:
This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of Java Programming Language, click the links and dive deeper into this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.