ProgrammingHardHCL · Tech Mahindra pattern

Assume 32-bit int. What is printed?

Assume 32-bit int. What is printed?
int arr[3] = {1, 2, 3};
int total = 0;
int i;
for (i = 0; i < 3; i++)
    total = total + arr[i];
printf("%d %d", total, i);
  1. A. 6 2
  2. B. 6 3
  3. C. 3 3
  4. D. 6 0

Correct answer: B. 6 3

The loop sums arr's elements: 1+2+3=6. i is declared OUTSIDE the loop here, so it retains its value after the loop exits: the loop's own increment runs one more time after the last true iteration (i becomes 3), and then the condition i<3 fails, exiting with i still at 3.

More C Programming questions

Exams that ask this topic