26 votes 26 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$ Programming in C gatecse-2008 programming programming-in-c easy + – Kathleen asked Sep 11, 2014 • edited Jan 12, 2023 by shadymademe Kathleen 12.6k views answer comment Share Follow See all 7 Comments See all 7 7 Comments reply Ahwan commented Aug 16, 2017 i edited by Ahwan Feb 7, 2019 reply Follow Share Note: " ? : " operator associativity is Right to Left. Only Assignment, Unary & ?: operator are right to left. Rest all are left to right. 6 votes 6 votes Gate Ranker18 commented Aug 17, 2017 reply Follow Share ( ) has highest precedence in above ques , ( ) comes into picture so associativity of ? : not counted 2 votes 2 votes Ahwan commented Aug 18, 2017 reply Follow Share 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. 5 votes 5 votes Chhotu commented Oct 25, 2017 i edited by Chhotu Oct 25, 2017 reply Follow Share Option B and C could be rejected directly because non of the variable X, Y or Z has value 4. 1 votes 1 votes Ashwani Kumar 2 commented Oct 23, 2018 reply Follow Share 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 votes 0 votes IITB2020 commented Feb 6, 2019 reply Follow Share @Ahwan link is not working. 0 votes 0 votes JashanArora commented Dec 28, 2019 reply Follow Share Option A: 4 Option B: 6 Option C: 6 Option D: 5 1 votes 1 votes Please log in or register to add a comment.
Best answer 29 votes 29 votes 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$ Keith Kr answered Sep 12, 2014 • edited Jan 22, 2018 by kenzou Keith Kr comment Share Follow See all 5 Comments See all 5 5 Comments reply Show 2 previous comments Subhankar Das commented Jan 22, 2017 reply Follow Share Yes , correct @sachin 0 votes 0 votes register_user_19 commented Dec 10, 2018 reply Follow Share 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 2 votes 2 votes IITB2020 commented Feb 6, 2019 reply Follow Share thanks 0 votes 0 votes Please log in or register to add a comment.
1 votes 1 votes If we look at the options carefully, Only Y has the value 4 , That means we will only go for the expression that results Y (i.e 3rd one) To execute expression-3, expression-1 has to be false. For this x<y , so option A will be true(i.e 3<4) Sanjiv Kumar answered Aug 10, 2023 Sanjiv Kumar comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes 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) Debargha Bhattacharj answered May 7, 2019 Debargha Bhattacharj comment Share Follow See all 0 reply Please log in or register to add a comment.