Consider the following ANSI C program. #include int main(){ int arr[4][5]; int i, j; for (i=0; i What is the output of the above program?
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2021 Set 2 · Q10
Time
#include <stdio.h>
int main(){
int arr[4][5];
int i, j;
for (i=0; i<4; i++){
for (j=0; j<5; j++){
arr[i][j] = 10*i + j;
}
}
printf("%d", *(arr[1] + 9));
return 0;
}What is the output of the above program?Select one answerSingle correct
Solution
Official answer: C) 24
Programming in CThe 2D array arr[4][5] has 4 rows and 5 columns, stored sequentially in row-major order in memory.
The nested loops initialize each element arr[i][j] to :
Row 0: arr[0][0] to arr[0][4] have values
Row 1: arr[1][0] to arr[1][4] have values
Row 2: arr[2][0] to arr[2][4] have values
Row 3: arr[3][0] to arr[3][4] have values
arr[1] decays to a pointer to the first element of the second row, i.e., &arr[1][0] (of type int *).
Pointer arithmetic arr[1] + 9 advances the pointer by int elements starting from arr[1][0]:
elements ahead from arr[1][0] corresponds to index: in flat 1D indexing.
Converting linear index back to 2D coordinates for an array with columns:
Thus, *(arr[1] + 9) accesses arr[2][4].
The value stored at arr[2][4] is:
Hence, the correct option is C.
Try the question first, then check your work.
Examiner trap
In C, multi-dimensional arrays are stored in row-major contiguous memory. Pointer arithmetic on arr[1] (which is of type int *) moves element by element across row boundaries.
This Programming in C previous year question is Q10 of the GATE CSE 2021 Set 2 paper. Find more GATE CSE PYQs on Programming in C in Programming and Data Structures, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Programming in C PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Programming in C has 74 GATE CSE PYQs across 25 papers from 2011 to 2026. 20 of them are 1-mark MCQs like this one, and GATE CSE 2021 Set 2 has 3.
How GATE tests Programming in CIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.