Consider the following statements int x = 10, y = 15; x = ((x = y) ? (y + x) :…
2025
Consider the following statements
int x = 10, y = 15;
x = ((x = y) ? (y + x) : (y - x));
What will be the value of x after executing these statements?
- A.
5
- B.
25
- C.
15
- D.
30
Attempted by 765 students.
Show answer & explanation
Correct answer: D
Key idea: the assignment expression returns the assigned value, which is used by the conditional.
Initial values: x = 10, y = 15.
Evaluate the conditional expression (x = y). This assigns 15 to x and the expression yields the value 15 (which is considered true since it is non-zero).
Because the condition is true, evaluate the true branch y + x using current values: y + x = 15 + 15 = 30.
Assign that result to x: x = 30.
Final answer: x becomes 30.
A video solution is available for this question — log in and enroll to watch it.