ProgrammingHardHCL · Tech Mahindra pattern

What is printed?

What is printed?
int a[6] = {1,2,3,4,5,6};
int *p = a + 1;
int *q = a + 4;
int diff = q - p;
q -= diff / 3 * 2;
printf("%d %d", diff, *q);
  1. A. 3 2
  2. B. 3 4
  3. C. 4 3
  4. D. 3 3

Correct answer: D. 3 3

diff = (a+4)-(a+1) = 3. diff/3*2 = 1*2 = 2 (integer division first: 3/3=1, then *2=2). q -= 2 moves q from a+4 to a+2, so *q = a[2] = 3.

More C Programming questions

Exams that ask this topic