52 52 votes Let $x$ be an integer which can take a value of $0$ or $1$. The statement if (x == 0) x = 1; else x = 0; is equivalent to which one of the following ? $x = 1 + x;$ $x = 1 - x;$ $x = x - 1;$ $x = 1\% x;$ Programming in C gateit-2004 programming easy identify-function + – Ishrat Jahan 17.8k views answer comment Share Follow Print See 1 comment 1 1 comment reply panipuri commented Jul 21 reply Follow flag basically finding 1's complement of xand 1's complement is nothing but 1 - x , as if x = 1 ; 1-1 = 0 and if x = 0; 1-0 = 1 0 0 replyShare Please log in or register to add a comment.
Best answer 66 66 votes Firstly, our requirement is for $x=1$ it makes '$0$' and for $x= 0$ it makes '$1$' Let's consider options one by one: $x= 1+x$ For $x = 1$, it gives $2$ So, False $x= 1- x$ Here, B is correct, as For $x= 0$, it gives $1$. For $x= 1$, it gives $0$. $x = x - 1$ For $x=0$ , it gives $-1$. So, False $x = 1 \% x$ For $x= 0$ , it gives $1 \% 0$ . I think it is undefined Even if we consider $x = x\%1 $ for $x= 0$ ,it gives $0\%1 = 0$ But we require $1$. So, Option (B) is correct. Himanshu1 answered Nov 16, 2015 • edited Jun 15, 2018 by Milicevic3306 Himanshu1 comment Share Follow See all 6 Comments 6 6 Comments reply Show 3 previous comments jacknroll commented Jun 6, 2024 reply Follow flag Dont assume its boolean if its not given 2 2 replyShare JethaG commented Nov 4, 2025 reply Follow flag 1 % 0 is undefined behavior 1 / 0 → undefined1 % 0 → also undefined 3 3 replyShare Nirajanandan samal commented Apr 25 reply Follow flag but it is given it will only 0 or 1 0 0 replyShare Please log in or register to add a comment.
3 3 votes option b. if x=0 we get x=1 if x=1 we get x=0.. 2 nd option satisfies this constraint Registered user 7 answered Jan 23, 2016 Registered user 7 comment Share Follow 0 reply Please log in or register to add a comment.
3 3 votes B is Correct.This code is nothing but flipping the value of X.If Content of X is 0 or 1 then we can flip it using following logicX = 1 - Xor X = X ^ 1 (^ is xor operation)orX = !x (! is logical not operation) So option B is the answer. omhari answered Nov 18, 2024 omhari comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes option D, as, x=1%x assigns remainder after dividing with 1 and hence x has to be 0 for getting remainder value anything other than 0.(as 1 divides every number>0 ) sumit kumar answered Nov 14, 2014 sumit kumar comment Share Follow See all 9 Comments 9 9 Comments reply Show 6 previous comments Lakshman Bhaiya commented Nov 27, 2018 reply Follow flag Yes i understand 0 0 replyShare tusharp commented Nov 27, 2018 reply Follow flag why (D) is wrong? ? 0 0 replyShare Lakshman Bhaiya commented Nov 27, 2018 reply Follow flag if(x=0) x=1; else x=0; $(D)$ x = 1% x when x=0 x = 1%0 i.e.$x=\frac{1}{0}=\infty$ it does not give any remainder. when x=1 x = 1%1 i.e.$x=\frac{1}{1}$ it gives remainder $= 0$. Both cases are not satisfied. So, this is false. 0 0 replyShare Please log in or register to add a comment.
0 0 votes option B is the correct answer x=1-x for x=0 :x=1-0=1for x=1:x=1-1=0no other options are matching the requirement Ekalavyaa answered Dec 6, 2024 Ekalavyaa comment Share Follow 0 reply Please log in or register to add a comment.