ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
#define ABS(x) x < 0 ? -x : x
printf("%d %d", ABS(2 - 5), ABS(-4) * 2);- A. 3 8
- B. -7 8
- C. -7 4
- D. 3 4
Correct answer: C. -7 4
Macros substitute tokens with no brackets. ABS(2 - 5) becomes 2 - 5 < 0 ? -2 - 5 : 2 - 5; the condition is true and -2 - 5 = -7. ABS(-4) * 2 becomes -4 < 0 ? - -4 : -4 * 2; the * 2 attaches only to the last branch, so the result is 4. The fix is #define ABS(x) ((x) < 0 ? -(x) : (x)).
More C Programming questions
- What is printed? c const char s = "mississippi"; printf("%td %td %td", strchr(s, 's') - s, strrchr(s, 's') - s, strstr(s, "ssip") - s);
- What is printed? c / 4-byte int / struct V { int a[3]; }; void f(struct V v) { v.a[0] = 9; } void g(int a[3]) { a[0] = 9; } int main(void)…
- What is printed? c int n = 10, c = 0; int k = (n + 3) / 4; switch (n % 4) { case 0: do { c++; case 3: c++; case 2: c++; case 1: c++; }…
- What is printed? c int calls = 0; int ways(int n) { calls++; if (n < 0) return 0; if (n == 0) return 1; return ways(n - 1) + ways(n - 3) +…
- What is printed? c int moves = 0; void h(int n, char from, char via, char to) { if (n == 0) return; h(n - 1, from, to, via); if (++moves…
- What is printed? c for (unsigned n = 5; n < 8; n++) printf("%u ", n ^ (n >> 1)); unsigned g = 13, b = 0; for (; g; g >>= 1) b ^= g…