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 : false
evaluates the conditiona > b
and assignstrue
if the condition is true; otherwise, it assignsfalse
.- Condition:
a > b
- Values:
a = 63
andb = 36
- Evaluation:
63 > 36
is true.
Thus,
x
will be assignedtrue
. - Condition:
-
Integer Expression for
y
:
int y = (a < b) ? a : b;
-
Similarly, the ternary operator
(condition) ? a : b
evaluates the conditiona < b
and assignsa
if the condition is true; otherwise, it assignsb
.- Condition:
a < b
- Values:
a = 63
andb = 36
- Evaluation:
63 < 36
is false.
Thus,
y
will be assignedb
, which is36
. - Condition:
Summary:
- The value of
x
istrue
. - The value of
y
is36
.
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.