ProgrammingModerateWipro · Capgemini pattern
What is printed?
What is printed?
int i, j, s = 0;
for (i = 0, j = 10; i < j; i += 2, j--)
s += j - i;
printf("%d %d %d", i, j, s);- A. 6 7 22
- B. 8 6 18
- C. 8 6 22
- D. 8 7 22
Correct answer: C. 8 6 22
Pairs (i, j): (0, 10) adds 10, (2, 9) adds 7, (4, 8) adds 4, (6, 7) adds 1 → 22. Then i = 8, j = 6 and the condition fails. The comma operator lets the for loop update two variables.
More C Programming questions
- What is printed? c unsigned int u = -16; / 32-bit unsigned int / printf("%d %u %d", 1 << 4, u >> 28, -16 / 4);
- What is printed? c int a = 5, b, c; b = a > 3 ? 10 : 20; c = (b == 10) ? a + b : 0; printf("%d %d %d", a, b, c);
- What is printed? c struct A { char c; int i; char d; }; struct B { int i; double d; }; struct C { char c; double d; char e; }; printf("%zu…
- What is printed? c char s = "hello"; printf("%c %c %c", s[1], 4[s], ("abc" + 2));
- What is printed? c int x = 10; int p = &x; int q = &p; q = 20; p += 5; printf("%d", x);
- What is printed? c #define MAX(a, b) ((a) > (b) ? (a) : (b)) int i = 3; int r = MAX(i++, 2); printf("%d %d", r, i);