The GATE Grind

GATE 2022 CS – Question 43

Programming and Data Structures · Programming in C · 2 marks · Multiple choice

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;
}
  1. 1 2 3 10 11 12 19 20 21
  2. 1 4 7 10 13 16 19 22 25
  3. 1 2 3 4 5 6 7 8 9
  4. 1 2 3 13 14 15 25 26 27

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (A) 1 2 3 10 11 12 19 20 21

Explanation

`int a[3][3][3] = {{1,...,9},{10,...,18},{19,...,27}}` initialises each plane `a[i]` (a 3×3 array) from one inner brace, in row-major order:
- `a[0]` rows: (1 2 3), (4 5 6), (7 8 9)
- `a[1]` rows: (10 11 12), (13 14 15), (16 17 18)
- `a[2]` rows: (19 20 21), (22 23 24), (25 26 27)

The loops use `j = 0` and vary `i` and `k`, printing `a[i][0][k]`, the first row of each plane:

1 2 3
10 11 12
19 20 21

Option (A).

More Programming in C questions

All Programming in C questions →