ProgrammingModerateTCS NQT · Accenture
How many times does the loop body run?
How many times does the loop body run?
#include <stdio.h>
int main(void) {
int c = 0;
for (int i = 0, j = 0; i < 4 && j < 4; i++, j += 2)
c++;
printf("%d", c);
}- A. 2
- B. 3
- C. 1
- D. 4
Correct answer: A. 2
The pairs (i, j) are (0, 0) and (1, 2), and then j reaches 4 so the test fails. The body runs twice.
More C Programming questions
- What is printed? c #include <stdio.h> void inc(int p, int n) { while (n--) (p++)++; } int main(void) { int a[] = {1, 2, 3}; inc(a, 2)…
- What is printed? c #include <stdio.h> int main(void) { unsigned short s = 1; s <<= 15; printf("%d", s); }
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 'a' < 'b' && 'b' < 'c'); }
- char d[4]; char s[] = "abc"; strncpy(d, s, sizeof d - 1); d[sizeof d - 1] = '0'; printf("%s", d); What is printed?
- char w[16]; scanf("%15s", w); Why is the width 15 used?
- int c = 0; const char s = "Queue"; for (int i = 0; s[i]; i++) if (strchr("aeiou", tolower((unsigned char)s[i]))) c++; printf("%d", c)…