ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
int a = 5, b = 3;
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("%d %d", a, b);- A. 5 3
- B. 6 5
- C. 3 6
- D. 3 5
Correct answer: D. 3 5
After a = a ^ b, a holds 5 ^ 3 = 6. b = 6 ^ 3 = 5 (the original a). a = 6 ^ 5 = 3 (the original b). The three XORs swap two DISTINCT variables without a temporary.
More C Programming questions
- What is printed? c printf("%d %d %d", strcmp("abc", "abd") < 0, strcmp("ab", "abc") < 0, strncmp("abcx", "abcy", 3) == 0);
- What is printed? c int i = 3.99; int j = -3.99; int k = (int)(0.29 100); printf("%d %d %d", i, j, k);
- What is printed? c / 4-byte int with 4-byte alignment / struct S { int a; char b; } arr[5]; printf("%td %td", &arr[4] - &arr[1], (char…
- What is printed? c char s[] = "abcdef"; char p = s + 2; p[1] = 'X'; printf("%s %s %c %zu", s, p, (p - 2), strlen(p + 4));
- What is printed? c int f(int n) { if (n <= 0) return 0; return n % 2 ? f(n - 1) + n : f(n / 2) 2; } int main(void) { printf("%d %d"…
- What is printed? c unsigned char x = 0x1D, r = 0; for (int i = 0; i < 8; i++) { r = (r << 1) | (x & 1); x >>= 1; } printf("%#x", r);