ProgrammingModerateProduct-company MCQ pattern
Given int x = 1, y = 2; const int *p = &x; int *const q = &x; which pair of statements both fail to compile?
- A. p = &y; and *q = 5;
- B. *p = 5; and q = &y;
- C. *p = 5; and *q = 5;
- D. p = &y; and q = &y;
Correct answer: B. *p = 5; and q = &y;
Read right to left: p is a pointer to const int, so the pointee cannot be changed through p (*p = 5 fails) but p can be re-pointed. q is a const pointer to int, so q cannot be re-pointed (q = &y fails) but *q can be changed.
More C Programming questions
- What is printed? c enum { A, B = 5, C, D = 2, E }; printf("%d %d %d", A, C, E);
- With 4-byte int, what is printed? c int a[] = {1, 2, 3}; char p = (char )a; p += 4; printf("%d", (int )p);
- What is printed? c int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; printf("%d %d", ((a + 1) + 2), a + 1);
- What is printed? c int x = 3; if (x = 0) printf("A%d", x); else printf("B%d", x);
- What is printed? c int add(int a, int b) { return a + b; } ... int (fp)(int, int) = add; printf("%d", (fp)(2, 3) + fp(4, 5));
- What is printed? c #define MAX(a, b) ((a) > (b) ? (a) : (b)) int x = 3, y = 5; int z = MAX(x++, y++); printf("%d %d %d", x, y, z);