What is printed by the following ANSI C program? #include<stdio.h> int…

2022

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. A.

    1 2 3
     10 11 12
     19 20 21

  2. B.

    1 4 7
     10 13 16
     19 22 25

  3. C.

    1 2 3
     4 5 6
     7 8 9

  4. D.

    1 2 3
     13 14 15
     25 26 27

Attempted by 122 students.

Show answer & explanation

Correct answer: A

Printed output:

1 2 3

10 11 12

19 20 21

Explanation:

  • The array is a[3][3][3], initialized as three 3x3 blocks: the first block contains 1..9 (arranged row-wise), the second 10..18, and the third 19..27.

  • Variables are initialized with j = 0 and never modified. The outer loop iterates i from 0 to 2, and the inner loop iterates k from 0 to 2.

  • Each iteration prints a[i][0][k] for k = 0,1,2, i.e. the first row of the i-th 3x3 block. Therefore the printed lines are the first rows of the three blocks: 1 2 3, then 10 11 12, then 19 20 21.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir