Consider the following code segment: int x = 22, y = 15; x = (x > y) ? (x + y)…
2018
Consider the following code segment: int x = 22, y = 15; x = (x > y) ? (x + y) : (x - y); What will be the value of x after the code is executed?
- A.
22
- B.
37
- C.
7
- D.
37 and 7
Attempted by 158 students.
Show answer & explanation
Correct answer: B
Key idea: The conditional (ternary) operator condition ? expr1 : expr2 evaluates expr1 when the condition is true, otherwise expr2. Given: x = 22, y = 15