Sunday, 2 September 2012

December 2010 - Question No 13

Which one of the following will set the value of y to 5 if x has the value 3, but not otherwise ?


(A) if (x=3)y=5
(B) if x==3(y=5)
(C) if (x==3);y=5
(D) if (x==3)y=5

ANS:-D

EXPLANATION:-
It is always tricky to answer such questions where options look similar. In such cases it is a better approach to remove all the apparent wrong options and then go for the tricky ones. In this question option A is ruled out because it is given as x=3, and = is not a comparison operator but a assignment operator and so x will be assigned the value of 3 and not compared. so A is ruled out. C is also ruled out because the presence of a ; at the end of (x==3) makes it as a independent statement and so the next statement which is y=5 will be executed irrespective of the result of the comparison. So C is ruled out.

Now we are left with only two options. It is either B or D. Since traditionally we enclose the operator to be compared within parenthesis i would choose D as the right answer. If anyone has any other explanation please post it to me.

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. The person who solved the Question he knows nothing about C. Plz don't misguide Students.

    (A) if (x=3)y=5 -->it is also correct it gives 5 as O/P
    (B) if x==3(y=5)-->Incorrect syntax
    (C) if (x==3);y=5 --->correct syntax it gives 5 as O/P
    (D) if (x==3)y=5 -->it is also correct it gives 5 as O/P

    A, C, D are correct ans. Question is incorrect becoz A, C, D are not in option

    for details contact: joni.wb1@gmail.com

    ReplyDelete