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);
}- A. 28
- B. 30
- C. 34
- 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
- How many pairs (i, j) with i < j satisfy a[i] < a[j] for a = {3, 1, 4, 2}? Compute with this program. c #include <stdio.h> int main(void)…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 3}, cnt[3], total = 0; for (int i = 0; i < 3; i++) { cnt[i] = 1…
- How many binary strings of length 5 contain no two adjacent 1s? Compute with this recursive program. c #include <stdio.h> int f(int n) {…
- What is printed? c #include <stdio.h> struct item { int w, v; }; int main(void) { struct item items[] = {{1, 10}, {2, 20}, {3, 30}}…
- What is printed? c #include <stdio.h> int main(void) { const char s = "hello"; printf("%c%c", s[1], (s + 4)); }
- What is printed? c #include <stdio.h> int main(void) { int x = 1, y = 2; const int cp = &x; int const pc = &y; pc = 5; printf("%d %d", cp…