**Note**: " ? : " operator associativity is **Right to Left**.

Only Assignment, Unary & ?: operator are right to left.

Rest all are left to right.

The Gateway to Computer Science Excellence

+19 votes

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)$$

- $x=3, y=4, z=2$
- $x=6, y=5, z=3$
- $x=6, y=3, z=5$
- $x=5, y=4, z=5$

+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

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.

+23 votes

Best answer

0 votes

Therefore, the correct option is the one with 4 as the max value.

Therefore, **correct opt: (A)**

0 votes

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)

⇒Answer is Option A.

→ 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)

⇒Answer is Option A.

52,345 questions

60,468 answers

201,792 comments

95,271 users