GateAI
Programming in C

GATE CSE 2021 Set 2 · Q10

MCQ1 markMedium

Time

Consider the following ANSI C program.#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

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) 24

Programming in C

The 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 10×i+j10 \times i + j:

  • Row 0: arr[0][0] to arr[0][4] have values 0,1,2,3,40, 1, 2, 3, 4

  • Row 1: arr[1][0] to arr[1][4] have values 10,11,12,13,1410, 11, 12, 13, 14

  • Row 2: arr[2][0] to arr[2][4] have values 20,21,22,23,2420, 21, 22, 23, 24

  • Row 3: arr[3][0] to arr[3][4] have values 30,31,32,33,3430, 31, 32, 33, 34

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 99 int elements starting from arr[1][0]:

  • 99 elements ahead from arr[1][0] corresponds to index: (1×5+0)+9=14(1 \times 5 + 0) + 9 = 14 in flat 1D indexing.

  • Converting linear index 1414 back to 2D coordinates for an array with 55 columns:

    Row=⌊14/5⌋=2\text{Row} = \lfloor 14 / 5 \rfloor = 2
    Column=14(mod5)=4\text{Column} = 14 \pmod 5 = 4

Thus, *(arr[1] + 9) accesses arr[2][4].

The value stored at arr[2][4] is:

10×2+4=2410 \times 2 + 4 = 24

Hence, the correct option is C.

Try the question first, then check your work.

Think the official key or this write-up is wrong?

Report error or challenge

Examiner trap

The misconception this question tests

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

Where do you stand for GATE?

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.

One GATE CSE PYQ every morning

An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.

Programming in C in GATE CSE

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 C

More from GATE CSE 2021 Set 2

Share this solution

A plain-text card for WhatsApp or Telegram study groups.

GateAI solution cardGATE CSE 2021 Set 2 · Q10 · MCQ

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

Independent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.