ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int i = 0;
int arr[5] = {10, 20, 30, 40, 50};
while (i < 5)
printf("%d ", arr[i++]);
printf("%d", i);- A. 10 20 30 40 50 5
- B. 10 20 30 40 5
- C. 10 20 30 40 50 4
- D. Undefined behaviour
Correct answer: A. 10 20 30 40 50 5
Each loop iteration is its own statement, so there's no unsequenced conflict: arr[i++] fully evaluates (reading the current i, then incrementing it) before the next iteration begins. The loop prints all 5 elements in order (10 20 30 40 50), and by the time it exits, i has been incremented 5 times to 5, which the final printf reports.
More C Programming questions
- What is printed? c int a = 5; int b = (a > 3) ? (a = 10) : (a = 20); printf("%d %d", a, b);
- What is printed? c int x = 2, y = 3, z = 4; int result = x + y z - x / y; printf("%d", result);
- Assume a 64-bit LP64 ABI. What is printed? c int arr[5] = {1, 2, 3, 4, 5}; int start = arr; int end = arr + 5; int sum = 0; for (int p =…
- Assume a 64-bit LP64 ABI. What is printed? c int x = 42; void vp = &x; int ip = (int )vp; printf("%d", ip);
- Assume a 64-bit LP64 ABI. What is printed? c int a[3] = {1, 2, 3}; int p = &a[1]; printf("%d %d", p[-1], p[1]);
- Assume a 64-bit LP64 ABI. What is printed? c int x = 5; int p = &x; int pp = &p; pp = NULL; printf("%d", p == NULL);