GATE / CS ARCHIVE

GATE CSE 2022 / QUESTION 43

GATE CSE 2022 Q43

Programming in C · Arrays & Strings · Full GATE CSE 2022 paper

GATE 2022, Q43 2 marks · MCQ
Report issue

Arrays & Strings

Question 43, GATE 2022: An ANSI C program initializes 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; then for(i=0;i<3;i++){for(k=0;k<3;k++) printf("%d ",a[i][j][k]); printf("\n");}. Find the rows printed. A (1,2,3)/(10,11,12)/(19,20,21); B (1,4,7)/(10,13,16)/(19,22,25); C (1,2,3)/(4,5,6)/(7,8,9); D (1,2,3)/(13,14,15)/(25,26,27).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

The initializer fills the 3×3×3 array in row-major order with 1 through 27. The middle index is fixed at j=0, while i and k vary. The selected values are a[0][0][k]=1, 2, 3; a[1][0][k]=10, 11, 12; a[2][0][k]=19, 20, 21. Thus the output is option A.

The address/value formula for this initializer is a[i][j][k]=9i+3j+k+1. Since j never changes from zero, each outer iteration jumps nine values, while each inner iteration advances one. The newlines occur after the three k values, producing exactly three rows.

Original paper, page 28 · Answer key