ProgrammingHardProduct-company MCQ pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
int a = 10, b = 20, c = 30;
int *ptrs[3] = {&a, &b, &c};
int **pp = ptrs;
for (int i = 0; i < 3; i++) {
*(*(pp + i)) += 100;
}
printf("%d %d %d", a, b, c);- A. 10 20 30
- B. 100 100 100
- C. 110 120 130
- D. Undefined behaviour: pp + i goes out of bounds
Correct answer: C. 110 120 130
pp points at the first element of the pointer array. *(pp + i) is ptrs[i] (the i-th stored address); dereferencing that again reaches the actual int (a, b, or c) and adds 100 to it. All three loop iterations stay within ptrs's valid bounds (i=0,1,2), so each variable is incremented exactly once: 110 120 130.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int (fp)(int, int); int add(int a, int b) { return a + b; } int sub(int a, int b) { return a…
- Assume a 64-bit LP64 ABI. What is printed? c void process(int data, int len) { for (int i = 0; i < len; i++) data[i] = 2; } int main(void)…
- Assume a 64-bit LP64 ABI. What is printed? c int global = 10; int getPtr(void) { return &global; } int main(void) { int p = getPtr(); p =…
- Assume a 64-bit LP64 ABI. What is printed? c struct Point { int x, y; }; void move(struct Point p, int dx, int dy) { p->x += dx; p->y +=…
- Assume a 64-bit LP64 ABI. What is printed? c void modify(int x) { x = 100; } void modifyPtr(int x) { x = 100; } int main(void) { int a =…
- Assume a 64-bit LP64 ABI. What is printed? c int allocateArray(int n) { int arr = malloc(n sizeof(int)); for (int i = 0; i < n; i++)…