ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
int a[3] = {5,10,15};
int *p = a;
int *q = &a[2];
while (p != q) {
printf("%d ", *p);
p++;
}
printf("%d", *p);- A. 5 10 5
- B. 5 15
- C. 5 10 15 15
- D. 5 10 15
Correct answer: D. 5 10 15
The loop prints while p hasn't reached a[2]: it prints a[0]=5 then a[1]=10, then p becomes &a[2] and the loop stops. The final printf outside the loop then prints *p = a[2] = 15. Output: 5 10 15.
More C Programming questions
- What is printed? c int a[3] = {1,2,3}; int p = a; int arrp[1] = {p}; int pp = arrp; (pp)[1] = 99; printf("%d %d %d", a[0], a[1], a[2]);
- What is printed? c int a[3] = {1,2,3}; int p = &a[1]; int x = p[1] - p[-1]; int y = (p+1) - (p-1); printf("%d %d", x, y);
- What is printed? c int add(int a,int b){ return a+b; } int sub(int a,int b){ return a-b; } int mul(int a,int b){ return ab; } int…
- What is printed? Assume a 64-bit LP64 ABI (4-byte int) and that the matrix is stored contiguously. c int a[2][3] = {{1,2,3},{4,5,6}}; int…
- What is printed? c int p = malloc(3 sizeof(int)); for (int i = 0; i < 3; i++) p[i] = malloc(3 sizeof(int)); for (int i = 0; i < 3; i++)…
- What is printed? c int r0[] = {1,2,3}; int r1[] = {4,5}; int r2[] = {6,7,8,9}; int jag[3] = {r0, r1, r2}; int lens[3] = {3,2,4}; int sum =…