The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+12 votes
1.4k views

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 ?

  1. $x = 1 + x;$
  2. $x  = 1 - x;$
  3. $x = x - 1;$
  4. $x = 1\% x;$
in Programming by Boss (16.3k points) | 1.4k views

3 Answers

+21 votes
Best answer

Firstly, our requirement is for $x=1$ it makes '$0$' and for $x= 0$ it makes '$1$'

Let's consider options one by one:

  1. $x= 1+x$
    • For $x = 1$, it gives $2$ So, False
       
  2. $x= 1- x$
    • Here, B is correct, as 
      • For $x= 0$, it gives $1$.
      • For $x= 1$, it gives $0$.
         
  3. $x = x - 1$    
    • For $x=0$ , it gives $-1$. So, False
       
  4. $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.

by Boss (15.5k points)
edited by
+1 vote
option b.

if x=0 we get x=1

if x=1 we get x=0..

2 nd option satisfies this constraint
by Junior (647 points)
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 laugh)

by Active (1.9k points)
0
it should be x%1 rt?
+1
sir it should be option B right?

x= 1-x

if we take x=0 den we will get x =1 and for x=1, we will get x=0
0
ya..arjun sir i think so...it should be x%1...
0
ya.. mitali... that can also be true if x is allowed to take a value of 0 or 1 only....
0
I have the doubt regarding option (D)

      x=1%x

if x=0,then we get x=1

x=1%0

x=1

and if x=1, then we get x=0

x=1%1

x=0

why $(D)$ is wrong?
0

x=1%0

It will not give 1. "Divide by 0 exception"  

0
Yes i understand
0

why (D) is wrong?

?  

0
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.

Answer:

Related questions

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
50,092 questions
55,322 answers
190,846 comments
86,219 users