ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
#define MAX(a, b) ((a) > (b) ? (a) : (b))
int x = 3, y = 5;
int z = MAX(x++, y++);
printf("%d %d %d", x, y, z);- A. 5 7 6
- B. 4 7 7
- C. 4 6 5
- D. 4 7 6
Correct answer: D. 4 7 6
The macro expands to ((x++) > (y++) ? (x++) : (y++)). The test compares 3 > 5 (false) and leaves x = 4, y = 6; ?: has a sequence point after the test, then only the chosen branch y++ runs, giving z = 6 and y = 7. A function would have given 4 6 5.
More C Programming questions
- What is printed? c int i = 0, j = 1, k; k = i++ && j++ || ++j; printf("%d %d %d", i, j, k);
- What is printed (the code compiles)? c int x = (5, 10, 15); int y; y = 5, 10; printf("%d %d", x, y);
- With 4-byte int, what is printed? c int i = 5; sizet s = sizeof(i++); printf("%d %zu", i, s);
- What is printed? c int a = -1; unsigned int b = 1; if (a < b) printf("less"); else printf("not less");
- What is printed? c float f = 0.1f, g = 0.5f; printf("%s ", f == 0.1 ? "eq" : "ne"); printf("%s", g == 0.5 ? "eq" : "ne");
- On a little-endian machine with 4-byte int and ASCII, what is printed? c union { int i; char c; } u; u.i = 0x41424344; printf("%c", u.c);