Skip to content
Official PYQ · 2008

GATE CS 2008 Q18 — Programming in C

Explain with AI

Which combination of the integer variables x,y,x, y, and zz makes the variable aa get the value 44 in the following expression?

a=(x>y)?((x>z)?x:z):((y>z)?y:z)a=(x > y)?((x > z) ?x:z): ((y > z) ?y:z)

Choose one option
View worked solution

Answer: A

The given expression uses nested ternary operators:

a=(x>y)?((x>z)?x:z):((y>z)?y:z)a = (x > y) ? ((x > z) ? x : z) : ((y > z) ? y : z)

Let's analyze the logic of this expression step-by-step:

  1. If x>yx > y is true:

    The expression evaluates to (x > z) ? x : z, which returns the maximum of xx and zz. Since x>yx > y, this effectively returns the maximum of all three variables, max⁡(x,y,z)\max(x, y, z).

  2. If x>yx > y is false (i.e., x≤yx \le y):

    The expression evaluates to (y > z) ? y : z, which returns the maximum of yy and zz. Since y≥xy \ge x, this also returns the maximum of all three variables, max⁡(x,y,z)\max(x, y, z).

Thus, the expression simplifies to finding the maximum of the three variables:

a=max⁡(x,y,z)a = \max(x, y, z)

We want a=4a = 4. Let's evaluate the options to find which set of values has a maximum of 44:

  • Option A: x=3,y=4,z=2  ⟹  max⁡(3,4,2)=4x=3, y=4, z=2 \implies \max(3, 4, 2) = 4. (Correct)
  • Option B: x=6,y=5,z=3  ⟹  max⁡(6,5,3)=6x=6, y=5, z=3 \implies \max(6, 5, 3) = 6.
  • Option C: x=6,y=3,z=5  ⟹  max⁡(6,3,5)=6x=6, y=3, z=5 \implies \max(6, 3, 5) = 6.
  • Option D: x=5,y=4,z=5  ⟹  max⁡(5,4,5)=5x=5, y=4, z=5 \implies \max(5, 4, 5) = 5.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions