The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+13 votes
896 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$
asked in Programming by Veteran (68.8k points) | 896 views

Note:   " ?  : " operator associativity is Right to Left.
Only Assignment, Unary &  ?:  operator are right to left.
Rest all are left to right.
http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

( ) has highest precedence

in above ques , ( ) comes into picture so associativity of ? : not counted
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.
Option B and C could be rejected directly because non of the variable X, Y or Z has value 4.

1 Answer

+20 votes
Best answer

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

answered by Boss (6.3k points)
selected by
Given expression finds maximum among x,y and z and assigns it to a.
Yes it is finding maximum between X,Y and Z and assign it to "A"..
Yes , correct @sachin


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

32,544 questions
39,231 answers
109,311 comments
36,613 users