2.2k views

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$
| 2.2k views
+5

Note:   " ?  : " operator associativity is Right to Left.
Only Assignment, Unary &  ?:  operator are right to left.
Rest all are left to right.

+1
( ) has highest precedence

in above ques , ( ) comes into picture so associativity of ? : not counted
+5
Yes,  here due to bracket, we need not care about the associativity of   ? :   But when there are multiple ?:  & no bracket is there then we should follow right to left.
+1
Option B and C could be rejected directly because non of the variable X, Y or Z has value 4.
0
Expression includes only comparison operator $>$, a will take value from x,y,z. That's why option B) and C) can be eliminated.

Check for option A) and D).... option A) satisfy
0

0

Option A: 4

Option B: 6

Option C: 6

Option D: 5

Using option (A) : $x=3, y=4, z=2$

$a=(3>4)?$ No

therefore don't evaluate the first part and check second part $((y>z)?y:z)$

$(4>2)?$ Yes

$a=$ value of $y =4$

Answer is (A) $x=3, y=4, z=2$

by
edited by
+13
Given expression finds maximum among x,y and z and assigns it to a.
0
Yes it is finding maximum between X,Y and Z and assign it to "A"..
0
Yes , correct @sachin
+1

convert to if-else

if(x>y)

{ if(x>z) a=x;

else a=z;

}

else{

if(y>z) a=y;

else a=z;

}

option:-

 x y z a 3 4 2 4 6 5 3 6 6 3 5 6 5 4 5 5

0
thanks

The given expression actually finds the max. of 3 integers.
Therefore, the correct option is the one with 4 as the max value.
Therefore, correct opt: (A)

Required final output value of a=4.
→ We can directly eliminate the options B & C, because none of the variable can assign a value 4.
→ Given explanation is
a=(x>y)?((x>z)?x:z):((y>z)?y:z)
Option A:
x=3; y=4; z=2
a=(3>4)?⇒No
Then evaluate second expression⇒(4>2)?Yes
⇒a=y
a=4 (True)
Option D:
x=5; y=4; z=5
a=(5>4)⇒Yes
Then evaluate first expression ⇒ (5>5)? No
⇒ a=z ⇒ a=5 (Not true)