GateAI
Linear Structures, Trees and Heaps

GATE CSE 2015 Set 3 · Q30

MCQ2 marksMedium

Time

Consider the following two C code segments. YY and XX are one and two dimensional arrays of size nn and n×nn \times n respectively, where 2≤n≤102 \le n \le 10. Assume that in both code segments, elements of YY are initialized to 0 and each element X[i][j]X[i][j] of array XX is initialized to i+ji+j. Further assume that when stored in main memory all elements of XX are in same main memory page frame. Code segment 1://initialize elements of Y to 0 //initialize elements X[i][j] of X to i+j for(i = 0; i < n; i++) Y[i] += X[0][i];Code Segment 2://initialize elements of Y to 0 //initialize elements X[i][j] of X to i+j for(i = 0; i < n; i++) Y[i] += X[i][0];Which of the following statements is/are correct? S1: Final contents of array YY will be same in both code segments S2: Elements of array XX accessed inside the for loop shown in code segment 1 are contiguous in main memory S3: Elements of array XX accessed inside the for loop shown in code segment 2 are contiguous in main memory

Select one answerSingle correct

Pick an option to check

Solution

Step-by-step verified solution

Official answer: C) Only S1 and S2 are correct

Linear Structures, Trees and Heaps

Let us analyze each statement:

  1. Statement S1:

    • In Code segment 1, for each index ii from 00 to n−1n-1:

      Y[i]=X[0][i]=0+i=iY[i] = X[0][i] = 0 + i = i
    • In Code segment 2, for each index ii from 00 to n−1n-1:

      Y[i]=X[i][0]=i+0=iY[i] = X[i][0] = i + 0 = i

    Since both code segments assign Y[i]=iY[i] = i for all 0≤i<n0 \le i < n, the final contents of array YY will be identical in both cases. Therefore, S1 is correct.

  2. Statement S2:

    • In C, two-dimensional arrays are stored in row-major order.

    • In Code segment 1, the loop accesses elements of the first row: X[0][0],X[0][1],…,X[0][n−1]X[0][0], X[0][1], \dots, X[0][n-1]. In row-major layout, all elements of a single row are stored contiguously in memory. Therefore, S2 is correct.

  3. Statement S3:

    • In Code segment 2, the loop accesses elements of the first column: X[0][0],X[1][0],…,X[n−1][0]X[0][0], X[1][0], \dots, X[n-1][0]. Consecutive elements in a column are separated by nn elements in row-major layout and are not contiguous. Therefore, S3 is incorrect.

Thus, only S1 and S2 are correct (Option 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 Linear Structures, Trees and Heaps previous year question is Q30 of the GATE CSE 2015 Set 3 paper. Find more GATE CSE PYQs on Linear Structures, Trees and Heaps 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 Linear Structures, Trees and Heaps 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.

Linear Structures, Trees and Heaps in GATE CSE

Linear Structures, Trees and Heaps has 14 GATE CSE PYQs across 12 papers from 2012 to 2026. 4 of them are 2-mark MCQs like this one, and it is the only one in GATE CSE 2015 Set 3.

How GATE tests Linear Structures, Trees and Heaps

More from GATE CSE 2015 Set 3

Share this solution

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

GateAI solution cardGATE CSE 2015 Set 3 · Q30 · MCQ

Consider the following two C code segments. Y and X are one and two dimensional arrays of size n and n × n respectively, where 2 ≤ n ≤ 10. Assume that in both code segments, elements of Y are initialized to 0 and each element X[i][j] of array X is initialized to i+j. Further assume that when stored in main memory all elements of X are in same main memory page frame. Code segment 1: //initialize elements of Y to 0 //initialize elements X[i][j] of X to i+j for(i = 0; i Code Segment 2: //initialize elements of Y to 0 //initialize elements X[i][j] of X to i+j for(i = 0; i Which of the following statements is/are correct? S1: Final contents of array Y will be same in both code segments S2: Elements of array X accessed inside the for loop shown in code segment 1 are contiguous in main memory S3: Elements of array X accessed inside the for loop shown in code segment 2 are contiguous in main memory

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.