ProgrammingModerate
A loop with i starting at 1 and i *= 2 until i > n runs how many times for n = 1000?
- A. 9
- B. 11
- C. 10
- D. 1000
Correct answer: C. 10
The values 1, 2, 4, ..., 512 are visited, which is 10 values; the next value 1024 exceeds 1000.
More C Programming questions
- A program uses a while loop that finds the smallest power of two at least n for n = 1000, starting from 1. What value does it return?
- int i = 0; for (; i < 5; ) i += 2; printf("%d", i); What is printed?
- int s = 0; for (int i = 1; i <= 4; i++) s += i (i % 2 ? 1 : -1); printf("%d", s); What is printed?
- int n = 0; while (++n < 10 && n % 4); printf("%d", n); What is printed?
- int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int s = 0; for (int i = 0; i < 3; i++) s += a[i][2 - i]; printf("%d", s); What is printed?
- int a[2][3] = {{7, 8, 9}, {4, 5, 6}}; int p = &a[0][0]; printf("%d", p[4]); What is printed?