ProgrammingHardTCS NQT · Infosys pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
int arr[5] = {1, 2, 3, 4, 5};
int *start = arr;
int *end = arr + 5;
int sum = 0;
for (int *p = start; p != end; p++) {
sum += *p;
}
printf("%d", sum);- A. 15
- B. 10
- C. Undefined behaviour: end points one past the array
- D. 0
Correct answer: A. 15
arr + 5 is exactly one-past-the-end of a 5-element array, which C explicitly permits forming (just never dereferencing). The loop walks p from start to (but not including) end, visiting all 5 real elements and summing them: 1+2+3+4+5=15.
More C Programming questions
- 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);
- Assume a 64-bit LP64 ABI. What is printed? c int a = 10; int p = &a; int q = p; p = NULL; printf("%d %d", p == NULL, q);
- Assume a 64-bit LP64 ABI. What is printed? c int rows = 2, cols = 3; int matrix = malloc(rows sizeof(int )); for (int i = 0; i < rows…
- Assume a 64-bit LP64 ABI. What is printed? c int x = 1; int p = &x; int pp = &p; int ppp = &pp; printf("%d", ppp == &pp && ppp == pp &&…