# Data Structure Questions and Answers – Matrix

This set of Data Structure Multiple Choice Questions & Answers (MCQs) focuses on “Matrix”.

1. What does the following piece of code do?

for(int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++)
{
if(i == j)
sum = sum + (array[i][j]);
}
}
System.out.println(sum);

A) Normal of a matrix
B) Trace of a matrix
C) Square of a matrix
D) Transpose of a matrix

Explanation: The sum of a matrix’s principal diagonal elements is called the trace.

2. If row-major order is used, how is the following matrix stored in memory?
a b c
d e f
g h i
A) ihgfedcba
B) abcdefghi

Explanation: It begins with the first element and continues in the same row until the row’s end is reached, after which it moves on to the next row. C is arranged in row-major order.

3. If column-major order is used, how is the following matrix stored in memory?
a b c
d e f
g h i
A) ihgfedcba
B) abcdefghi

Explanation: It begins with the first part and continues in the same column until the column’s end is reached, after which it moves on to the next column. Fortran uses a column-major ordering system.

4. Which of the following don’t use matrices?
A) In solving linear equations
B) Image processing
C) Graph theory
D) Sorting numbers

Explanation: For sorting, Numbers uses arrays (1-D) rather than matrices (2-D arrays). Solving linear equations with matrices is a different field of mathematics, whereas image processing stores pixels as matrices, and graphs are represented using matrices to denote nodes and edges.

5. Which of the following is an advantage of matrices?
A) Internal complexity
B) Searching through a matrix is complex
C) Not space efficient
D) Graph Plotting

Explanation: Adjacency and Incidence Matrices are used to store vertices and edges of a graph. It is an advantage to plot graphs easily using matrices. But Time complexity of a matrix is O(n2) and sometimes the internal organization becomes tedious. They are all disadvantages of matrices.

6. Matrix A when multiplied with Matrix C gives the Identity matrix I, what is C?
A) Identity matrix
B) Inverse of A
C) Square of A
D) Transpose of A

Explanation: For every square matrix is multiplied by its inverse, the identity matrix is obtained. It’s worth noting that non-square matrices can’t be inverted.

7. What is the order of a matrix?
A) number of rows X number of columns
B) number of columns X number of rows
C) number of rows X number of rows
D) number of columns X number of columns

Explanation: The number of rows X number of columns determines the matrix’s order.

8. Which of the following property does not hold for matrix multiplication?
A) Associative
B) Distributive
C) Commutative

Explanation: In matrix multiplication, AB != BA

9. How do you allocate a matrix using a single pointer in C?(r and c are the number of rows and columns respectively)
a) int *arr = malloc(r * c * sizeof(int));
b) int *arr = (int *)malloc(r * c * sizeof(int));
c) int *arr = (int *)malloc(r + c * sizeof(int));
d) int *arr = (int *)malloc(r * c * sizeof(arr));

Explanation: Total number of elements in the matrix will be r*c

10. Select the code snippet which performs matrix multiplication.(a and b are the two given matrices, resultant marix is c)
A)

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}

B)

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] * a[i][k] * b[k][j];
}
}
}

C)

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][k] + b[k][j];
}
}
}

D)

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < n; k++)
{
c[i][j] = c[i][j] + a[i][j] + b[k][j];
}
}
}

Explanation: A cumulative sum is generated by multiplying the corresponding elements from the row and column.

A data structure is a data organisation, management, and storage format in computer science that allows for efficient access and modification. A data structure, more specifically, is a list of data values, their relationships, and the functions or operations that can be performed on the data. For applications such as massive databases and internet indexing services, data structures offer a way to effectively handle large volumes of data. In certain cases, designing effective algorithms necessitates the use of efficient data structures.