ProgrammingVery hard
int a = 0; for (int i = 0; i < 1000000; i++) a ^= i; printf("%d", a); What is printed?
- A. 1000000
- B. 999999
- C. 0
- D. 1
Correct answer: C. 0
The XOR of 0 through n is 0 when n mod 4 == 3, and 999999 mod 4 is 3, so the result is 0.
More C Programming questions
- int x = 1, c = 0; while (x < 1000000000) { x = 3; c++; } printf("%d", c); What is printed (no overflow occurs)?
- A linear search over an array of length n finds the value only at the last position. How many comparisons does it make?
- Bubble sort with a swapped flag stops early. On an already sorted array of length n, how many passes run?
- A recursive binary search on n = 1000 sorted values reaches what maximum recursion depth?
- Merge sort on n = 2^20 elements has what recursion depth?
- An algorithm does O(n) work on each of the n levels of a recursion that removes one element per level. Which total is correct?