ProgrammingHardProduct-company MCQ pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
int rows = 2, cols = 3;
int **matrix = malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++) matrix[i] = malloc(cols * sizeof(int));
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
matrix[i][j] = i * cols + j;
printf("%d %d", matrix[1][2], *(*(matrix + 1) + 2));- A. 5 5
- B. 4 5
- C. 5 4
- D. 4 4
Correct answer: A. 5 5
This is a true dynamically-allocated jagged array of pointers (NOT a contiguous 2D block): matrix[1][2] = 1*3+2 = 5. *(*(matrix+1)+2) is exactly the same access written in pointer-arithmetic form: matrix[1] gives the second row pointer, +2 then dereferenced gives the same element, 5.
More C Programming questions
- Assume a 64-bit LP64 ABI. What is printed? c int a = 10; int p = &a; p += 5; p = p; p = 2; printf("%d", a);
- Assume a 64-bit LP64 ABI. What is printed? c int a[2][2] = {{1, 2}, {3, 4}}; int p = a[0]; int q = a[1]; printf("%ld", q - p);
- Assume a 64-bit LP64 ABI. What is printed? c int a = 1; int b = 2; int parr[2]; parr[0] = &a; parr[1] = &b; int pp = parr; for (int i = 0…
- What is printed? c void f(int n) { if (n == 0) return; printf("%d ", n); f(n - 1); printf("%d ", n); } int main(void) { f(3); }
- What is printed? c int f(int n, int acc) { if (n == 0) return acc; return f(n - 1, acc n); } int main(void) { printf("%d", f(5, 1)); }
- What is printed? c int f(int n) { int local = n; if (n == 0) return 0; local += f(n - 1); return local; } int main(void) { printf("%d"…