Question
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("\n");
}
return 0;
}Official answer and solution
- Official answer
- A
- From
- Official GATE 2022 answer key
Solution
Trace the array indices accessed in the nested loops.
The array is initialized with 27 elements:
- Slice : rows of 3 elements:
- Slice : rows of 3 elements:
- Slice : rows of 3 elements:
The loop variables are (outer loop from 0 to 2) and (inner loop from 0 to 2), while remains fixed at :
- Outer loop : prints
- Outer loop : prints
- Outer loop : prints
Each iteration of the outer loop concludes with a newline, printing:
1 2 3
10 11 12
19 20 21
This matches Option (A).
Option (A)
Assuming increments with or , whereas is constant at 0 throughout.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2022 Computer Science and Information Technology (CS)
- Question
- 43 of 65
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.