ProgrammingHard
int i = 0; for (; i < 5; ) i += 2; printf("%d", i); What is printed?
- A. 6
- B. 4
- C. 3
- D. 10
Correct answer: A. 6
i goes 0, 2, 4, 6 and the test 6 < 5 fails, so i overshoots the bound and 6 is printed. 4 is the last value that passed the test, 3 is the number of iterations, and 10 assumes the loop runs five times.
More C Programming questions
- 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?
- int a[2][2] = {{1, 2}, {3, 4}}; int t = a[0][0]; a[0][0] = a[1][0]; a[1][0] = t; printf("%d", a[0][0]); What is printed?
- int a[2][2] = {{1, 2}, {3, 4}}; int (r)[2] = a + 1; printf("%d", r[0][1]); What is printed?