ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int c = 0;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (j == i) continue;
if (j > i + 1) break;
c++;
}
}
printf("%d", c);- A. 15
- B. 14
- C. 10
- D. 20
Correct answer: B. 14
For each i, j counts 0 … i + 1 except j = i, capped at 4. That is 1, 2, 3, 4 for i = 0 to 3, and 4 for i = 4 (j = 0 to 3; j = 5 is never reached). Total 14. '15' forgets the cap at the last row.
More C Programming questions
- Which of the following is NOT allowed in C?
- What is printed? c int cnt(int n, int last) { if (n == 0) return 1; return cnt(n - 1, 0) + (last ? 0 : cnt(n - 1, 1)); } int main(void) {…
- On a two's-complement machine, what is printed? c int a = 5; printf("%d %d %d", !a, ~a & 0xF, !!a + !~-1);
- What is printed? c const char s = "a1b22c333"; int sum = 0, cur = 0; for (; s; s++) { if (s >= '0' && s <= '9') cur = cur 10 + (s - '0')…
- What is printed? c typedef int (Op)(int); int twice(int x) { return 2 x; } int plus3(int x) { return x + 3; } int apply(Op f, Op g, int x)…
- What is printed? c for (int k = 0; k < 3; k++) { static int s[3]; int a[3] = {0}; s[k] = a[k] = k + 1; printf("%d%d ", s[0] + s[1] + s[2]…