Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. 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. x = 3, y = 4, z = 2
    2. x = 6, y = 5, z = 3
    3. x = 6, y = 3, z = 5
    4. x = 5, y = 4, z = 5
Correct Option: A

The operator “?:” in C is the ternary operator which means that, if the expression is exp 1? exp2: exp3, so it means, if exp1 is true then exp2 is returned as the answer else exp3 is the required answer.
So, in the given expression let us consider x = 3, y = 4, z = 2,
Then, expression becomes a = (3 > 4)? ((3 > 2)? 3:2): ((4>2?4:2)
From this, we get that 3>4 is false so we go for the else part of the statement which is 4>2 and is true thus, the answer is 4, the true part of the statement.



Your comments will be displayed only after manual approval.