Data Structure Questions and Answers – Matrix

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

1. 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
View Answer

Answer: a
Explanation: The order of the matrix is the number of rows X number of columns.

2. Which of the following property does not hold for matrix multiplication?
a) Associative
b) Distributive
c) Commutative
d) Additive Inverse
View Answer

Answer: c
Explanation: In matrix multiplication, AB != BA

3. 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));
View Answer

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

4. 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)

advertisement
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)

advertisement
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];
        }
    }
}
View Answer
Answer: a
Explanation: The corresponding elements from the row and column are multiplied and a cumulative sum is formed.
 
 

5. 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
View Answer

Answer: b
Explanation: Trace of a matrix is the sum of the principal diagonal elements.

6. 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
c) cfibehadg
d) adgbehcfi
View Answer

Answer: b
Explanation: It starts with the first element and continues in the same row until the end of row is reached and then proceeds with the next row. C follows row-major order.

7. 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
c) cfibehadg
d) adgbehcfi
View Answer

Answer: d
Explanation: It starts with the first element and continues in the same column until the end of column is reached and then proceeds with the next column. Fortran follows column-major order.

8. Which of the following don’t use matrices?
a) In solving linear equations
b) Image processing
c) Graph theory
d) Sorting numbers
View Answer

Answer: d
Explanation: Numbers uses arrays(1-D) for sorting not matrices(2-D arrays). Solving linear equations is a separate field in Mathematics involving matrices, Image processing stores the pixels in the form of matrices, and the graphs are represented with the help of matrices to indicate the nodes and edges.

9. 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
View Answer

Answer: d
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.

10. 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
View Answer

Answer: b
Explanation: Any square matrix when multiplied with its inverse gives the identity matrix. Note that non square matrices are not invertible.

Sanfoundry Global Education & Learning Series – Data Structure.

To practice all areas of Data Structure, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.