Data Structures & AlgorithmsModerateCognizant · Capgemini pattern

What is printed? (2-D array in row-major order)

What is printed? (2-D array in row-major order)
R, C = 3, 4
base, size = 1000, 4
def addr(i, j, row_major=True):
    return base + size * (i * C + j if row_major else j * R + i)
print(addr(2, 1), addr(2, 1, False))
  1. A. 1036 1020
  2. B. 1036 1028
  3. C. 1020 1036
  4. D. 1024 1020

Correct answer: A. 1036 1020

Row-major: offset = i·C + j = 2·4 + 1 = 9 elements → 1000 + 36. Column-major: offset = j·R + i = 1·3 + 2 = 5 → 1000 + 20. C and Python (NumPy default) are row-major; Fortran is column-major.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic