ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int m[4][4];
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            m[i][j] = i * 4 + j;
    int s = 0;
    for (int i = 0; i < 4; i++) s += m[i][i];
    printf("%d", s);
}
  1. A. 28
  2. B. 30
  3. C. 34
  4. D. 24

Correct answer: B. 30

m[i][i] = 4i + i = 5i, so the diagonal holds 0, 5, 10, 15 and sums to 30. 24 forgets the + j part and sums only the row starts 4i.

More C Programming questions

Exams that ask this topic