Redirected
edited by
10,080 views
11 11 votes

The for loop

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

prints

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

5 Answers

3 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 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 .
1 1 vote
i&1 is basically checking whether i is even or odd.If it's even it'll return 1 else 0. Based on this I think output should be A.
Answer:
Position:
Show:

Related questions

11 11 votes
4 answers 4 answers
12.3k
12.3k views
go_editor asked Jun 21, 2016
12,325 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 ...
9 9 votes
2 answers 2 answers
10.7k
10.7k views
go_editor asked Jun 21, 2016
10,712 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
9 9 votes
3 answers 3 answers
8.9k
8.9k views
go_editor asked Jun 21, 2016
8,921 views
The following programmain() { inc(); inc(); inc(); } inc() { static int x; printf("%d", ++x); }prints 012prints 123prints 3 consecutive, but unpredictable numbersprints 1...
17 17 votes
8 answers 8 answers
14.5k
14.5k views
milankamilya asked Jun 14, 2016
14,506 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