Similar questions 5 more →
Arrays & Strings
![Question 50, GATE 2015: Y is a one-dimensional array of size n and X a C two-dimensional n×n array, with 2≤n≤10. Each segment starts with Y[i]=0 and X[i][j]=i+j. All X elements lie in the same main-memory page frame. Segment 1: for(i=0;i<n;i++) Y[i]+=X[0][i]; Segment 2: for(i=0;i<n;i++) Y[i]+=X[i][0]; Assess S1: final Y contents are identical; S2: X elements accessed in segment 1 are contiguous in memory; S3: X elements accessed in segment 2 are contiguous. Options: (A) S2 only; (B) S3 only; (C) S1 and S2 only; (D) S1 and S3 only.](../../assets/questions/CS-2015-S3-Q50-1.webp)
Your answer
Answer & solution
Official answer: C
1. Both loops add i to initially zero Y[i], because X[0][i]=0+i and X[i][0]=i+0.
2. In C row-major storage, X[0][i] traverses consecutive elements, whereas X[i][0] advances by n elements for n≥2. Being in one page does not make those column elements adjacent. Thus S1, S2, C.