ProgrammingHard
int c = 0, n = 3; do { c++; } while (n-- > 0); printf("%d", c); What is printed?
- A. 3
- B. 2
- C. 4
- D. 5
Correct answer: C. 4
The body runs first, and the test n-- > 0 is true for n = 3, 2 and 1. It is false when n is 0, so the body has run four times.
More C Programming questions
- int c = 0; for (int i = 0; i < 5; i++) { if (i == 2) continue; if (i == 4) break; c += i; } printf("%d", c); What is printed?
- int c = 0; for (int i = 0; i < 100; i++) c += i % 3 == 0; printf("%d", c); What is printed?
- unsigned n = 5; int c = 0; while (n-- > 0) c++; printf("%d", c); What is printed?
- int a = 0; for (int i = 0; i < 1000000; i++) a ^= i; printf("%d", a); What is printed?
- int x = 1, c = 0; while (x < 1000000000) { x = 3; c++; } printf("%d", c); What is printed (no overflow occurs)?
- A linear search over an array of length n finds the value only at the last position. How many comparisons does it make?