ProgrammingHardTCS NQT · Infosys
What is the output?
What is the output?
int i = 0, s = 0;
do {
i++;
if (i % 2) continue;
s += i;
} while (i < 7);
printf("%d %d", i, s);- A. Nothing: it loops forever
- B. 6 12
- C. 8 20
- D. 7 12
Correct answer: D. 7 12
In a do-while, continue jumps to the condition test, not back to the top, so the loop still ends when i reaches 7. Only the even values 2, 4, 6 are added: s = 12. The infinite-loop option assumes continue skips the test.
More C Programming questions
- What is printed? c char label(int n) { static char buf[8]; buf[0] = 'A' + n; buf[1] = '0'; return buf; } int main(void) { char a =…
- What is printed? c int i, s = 0; for (i = 1; i < 50; i = i 2 + 1) s += i % 3 ? i : -i; printf("%d %d", s, i);
- Assume ASCII. What is printed? c char s[] = "aB3"; for (char p = s; p; p++) p ^= 32; printf("%d %d %d", s[0], s[1], s[2]);
- What is printed? c void f(int p, int n) { if (n == 0) return; p += n; f(p, n - 1); p = 2; } int main(void) { int x = 0; f(&x, 3)…
- On an LP64 system (8-byte pointers), what is printed? c printf("%zu %zu", sizeof("abc"), sizeof("abc" + 1));
- On an LP64 system (8-byte pointers), what is printed? c char a[] = "cat", b = "cat"; printf("%d %d", strcmp(a, b) == 0, sizeof(a) ==…