edited by
6,119 views
9 votes
9 votes

The for loop

for (i=0; i<10; ++i)
printf("%d", i&1);

prints

  1. 0101010101
  2. 0111111111
  3. 0000000000
  4. 1111111111
edited by

5 Answers

Best answer
18 votes
18 votes
Option A is Ans.

Bcz It is Bitwise And Operation.

   i      &    1

0000 & 0001=0

0001 & 0001=1

0010 & 0001=0

0011 & 0001=1

Here the result is 1 if LSB of i is 1(Odd no.) & it occur at alternative position

So Ans is 0101010101

http://www.tutorialspoint.com/cprogramming/c_bitwise_operators.htm
edited by
7 votes
7 votes

(odd no) & 1 = 1

(even no) &1 = 0

Ans- A. 0101010101

3 votes
3 votes
Answer is A.the loop runs from 0 to 9 and i&1 MEANS BITWISE AND WITH THE BINARY FORMS OF 0 TO 9 WITH 1.SO THE NUMBERS HAVING LAST BIT AS 0 GIVES OUTPUT 0 AND NUMBERS HAVING LAST BIT AS 1 GIVES OUTPUT 1.
3 votes
3 votes
Here key point is that for bitwise op to be 1 we must have AND operation of LSB to be 1 here since we have bitwise AND operation performed with 1 whose LSB is 1 therefore all the odd numbers shall have their LSB 's to be 1 so directly we can say that the result of bitwise AND would be 0 for all even numbers and 1 for all odd numbers.so op is option A .
Answer:

Related questions

10 votes
10 votes
4 answers
1
go_editor asked Jun 21, 2016
9,377 views
The output of the following program ismain() { static int x[] = {1,2,3,4,5,6,7,8} int i; for (i=2; i<6; ++i) x[x[i]]=x[i]; for (i=0; i<8; ++i) printf("%d", x[i]); }1 2 3 ...
8 votes
8 votes
2 answers
2
go_editor asked Jun 21, 2016
7,630 views
Consider the following program fragmenti=6720; j=4; while (i%j)==0 { i=i/j; j=j+1; }On termination j will have the value4896720
7 votes
7 votes
3 answers
3
go_editor asked Jun 21, 2016
6,607 views
The following programmain() { inc(); inc(); inc(); } inc() { static int x; printf("%d", ++x); }prints 012prints 123prints 3 consecutive, but unpredictable numbersprints 1...
13 votes
13 votes
8 answers
4
milankamilya asked Jun 14, 2016
8,441 views
If n has 3, then the statement a[++n]=n++;assigns 3 to a[5]assigns 4 to a[5]assigns 4 to a[4]what is assigned is compiler dependent