Exams › GATE › Technical
What is printed by the following ANSI C program?
#include<stdio.h>
int main(int argc, char *argv[])
{
int a[3][3][3] =
{{1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18},
{19, 20, 21, 22, 23, 24, 25, 26, 27}};
int i = 0, j = 0, k = 0;
for( i = 0; i < 3; i++){
for(k = 0; k < 3; k++)
printf("%d ", a[i][j][k]);
printf("
");
}
return 0;
}
- 1 2 3
10 11 12
19 20 21
- 1 4 7
10 13 16
19 22 25
- 1 2 3
4 5 6
7 8 9
- 1 2 3
13 14 15
25 26 27
Correct answer: 1 2 3
10 11 12
19 20 21
Solution
The program initializes a 3D array and uses nested loops to print the elements. The outer loop iterates over the first dimension, while the inner loop accesses the second dimension with a fixed index of 'j' (which is always 0), resulting in the first slice of the array being printed for each iteration.
Related GATE Technical questions
⚔️ Practice GATE Technical free + battle 1v1 →