Which combination of the integer variables x, y and z makes the variable a get…

2008

Which combination of the integer variables x, y and z makes the variable a get the value 4 in the following expression?
a = ( x > y ) ? (( x > z ) ? x : z) : (( y > z ) ? y : z )

  1. A.

    x = 3, y = 4, z = 2

  2. B.

    x = 6, y = 5, z = 3

  3. C.

    x = 6, y = 3, z = 5

  4. D.

    x = 5, y = 4, z = 5

Attempted by 473 students.

Show answer & explanation

Correct answer: A

Key idea: the ternary expression returns the maximum of x, y and z by first comparing x and y, then comparing the larger of those with z.

  • If x > y, the expression chooses the larger of x and z (so it yields max(x, z)).

  • If x <= y, the expression chooses the larger of y and z (so it yields max(y, z)).

  • Therefore the overall expression yields the maximum of x, y and z.

Apply to x = 3, y = 4, z = 2: x > y is false (3 > 4 is false), so evaluate (y > z) ? y : z. Since 4 > 2 is true, a = 4.

Conclusion: The combination x = 3, y = 4, z = 2 makes a equal to 4.

Explore the full course: Gate Guidance By Sanchit Sir