ProgrammingHardTCS NQT · Infosys
#define HALF(x) x / 2. With int a = 5, b = 3, what does printf("%d", HALF(a + b)) print?
- A. 4
- B. 3
- C. 6
- D. 5
Correct answer: C. 6
The expansion is a + b / 2, which is 5 + 1 = 6. The programmer intended (a + b) / 2 = 4, so the macro needs parentheses around x.
More C Programming questions
- char s[] = "2024"; int sum = 0; for (int i = 0; s[i] != '0'; i++) sum += s[i] - '0'; What is sum?
- int count = 0; for (int i = 1; i <= 10; i++) for (int j = i; j <= 10; j += i) count++; What is count at the end?
- A contest program must test whether n is prime for n up to 10^9 with a 1 second limit. Which method is fast enough for one query?
- How many elements in the 1 to 50 range have exactly three 1 bits in binary?
- char buf[5] = "abcd"; printf("%zu", sizeof(buf) - strlen(buf)); What is printed?
- int a[5] = {0}; int p = a + 5; p[-1] = 7; printf("%d", a[4] + (a + 4)); What is printed?