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?

  1. A.

    22

  2. B.

    37

  3. C.

    7

  4. 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

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs