17,576 views
50 50 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 ?

  1. $x = 1 + x;$
  2. $x  = 1 - x;$
  3. $x = x - 1;$
  4. $x = 1\% x;$

5 Answers

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:

  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.

edited by
2 2 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 logic

X = 1 - X
or

X = X ^ 1      (^ is xor operation)
or
X = !x         (! is logical not operation)
 

So option B is the answer.

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 laugh)

0 0 votes

option B is the correct answer 

x=1-x 

for x=0 :

x=1-0

=1

for x=1:

x=1-1

=0

no other options are matching the requirement

Answer:
Position:
Show:

Related questions

56 56 votes
6 answers 6 answers
19.2k
19.2k views
Ishrat Jahan asked Nov 2, 2014
19,226 views
Consider the following C program which is supposed to compute the transpose of a given $4 \times 4$ matrix $M$. Note that, there is an $X$ in the program which indicates ...
50 50 votes
3 answers 3 answers
15.9k
15.9k views
Ishrat Jahan asked Nov 2, 2014
15,853 views
Consider the following C program:#include <stdio.h typedef struct { char *a; char *b; } t; void f1 (t s); void f2 (t *p); main() { static t s = {"A", "B"}; printf ("%s %s...
35 35 votes
3 answers 3 answers
11.7k
11.7k views
Ishrat Jahan asked Nov 2, 2014
11,727 views
Choose the correct option to fill the $?1$ and $?2$ so that the program prints an input string in reverse order. Assume that the input string is terminated by a new line ...
60 60 votes
3 answers 3 answers
15.7k
15.7k views
Ishrat Jahan asked Nov 2, 2014
15,712 views
What is the output of the following program?#include<stdio.h int funcf (int x); int funcg (int y); main () { int x = 5, y = 10, count; for (count = 1; count <= 2; ++count...