ProgrammingModerate
int cnt = 0; for (int i = 0; i < 10; i += 3) cnt++; printf("%d", cnt); What is printed?
- A. 4
- B. 3
- C. 5
- D. 10
Correct answer: A. 4
i takes 0, 3, 6 and 9; the next value 12 fails i < 10, so the body runs 4 times, i.e. ceil(10 / 3). 3 is floor(10 / 3), which forgets the pass at i = 0.
More C Programming questions
- int i = 0, j = 10, cnt = 0; while (i < j) { i++; j--; cnt++; } printf("%d", cnt); What is printed?
- unsigned int i; for (i = 10; i >= 0; i--) { } What is the behaviour of this loop?
- Which C code correctly counts the digits of n = 10^9 in a fast enough way for many queries?
- The sum of the digits of n = 10^9 - 1 is computed with a loop that divides by 10 each time. How many loop iterations run?
- To list all primes up to 10^7 in C with memory and time that fit, which is the best choice?
- For each of Q = 10^5 queries, a program sums an array prefix of length up to N = 10^5 by a loop. What is the total cost?