Skip to content
Official PYQ · 2022

GATE CS 2022 Q33 — Arrays, Stacks and Queues

Explain with AI

What is printed by the following ANSI C\text{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;

}

Choose one option
View worked solution

Answer: A

The 3D array a[3][3][3] consists of 33 two-dimensional arrays, each of size 3×33 \times 3. The initialization assigns values row-by-row to these sub-arrays:

  • For i=0i = 0: a[0] is initialized with values 11 to 99. a[0][0]={1,2,3},a[0][1]={4,5,6},a[0][2]={7,8,9}a[0][0] = \{1, 2, 3\}, \quad a[0][1] = \{4, 5, 6\}, \quad a[0][2] = \{7, 8, 9\}
  • For i=1i = 1: a[1] is initialized with values 1010 to 1818. a[1][0]={10,11,12},a[1][1]={13,14,15},a[1][2]={16,17,18}a[1][0] = \{10, 11, 12\}, \quad a[1][1] = \{13, 14, 15\}, \quad a[1][2] = \{16, 17, 18\}
  • For i=2i = 2: a[2] is initialized with values 1919 to 2727. a[2][0]={19,20,21},a[2][1]={22,23,24},a[2][2]={25,26,27}a[2][0] = \{19, 20, 21\}, \quad a[2][1] = \{22, 23, 24\}, \quad a[2][2] = \{25, 26, 27\}

In the nested loop, the variable j remains constant at 00. The loop prints a[i][0][k] for i∈{0,1,2}i \in \{0, 1, 2\} and k∈{0,1,2}k \in \{0, 1, 2\}:

  • When i=0i = 0: Prints a[0][0][0], a[0][0][1], a[0][0][2]   ⟹  \implies 1 2 3
  • When i=1i = 1: Prints a[1][0][0], a[1][0][1], a[1][0][2]   ⟹  \implies 10 11 12
  • When i=2i = 2: Prints a[2][0][0], a[2][0][1], a[2][0][2]   ⟹  \implies 19 20 21

Thus, the correct option is A.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Arrays, Stacks and Queues questions