MATLAB General MCQ 2 — Questions and Answers
Question 1: Which MATLAB function returns the size of each dimension of an array?
- length(A)
- size(A) (Correct answer)
- numel(A)
- ndims(A)
Correct answer: size(A)
size(A) returns a row vector whose elements are the lengths of the corresponding dimensions of A.
Question 2: What does the MATLAB expression A(end, :) return?
- The first row of A
- The last column of A
- The last row of A (Correct answer)
- The last element of A
Correct answer: The last row of A
The keyword 'end' refers to the last index in a dimension, so A(end, :) selects the last row and all columns.
Question 3: Which operator performs element-wise multiplication in MATLAB?
- *
- .* (Correct answer)
- ×
- .×
Correct answer: .*
The .* operator multiplies corresponding elements of two arrays, whereas * performs matrix multiplication.
Question 4: What is the output of mod(10, 3) in MATLAB?
- 3
- 1 (Correct answer)
- 0
- 2
Correct answer: 1
mod(10, 3) returns 1, the remainder when 10 is divided by 3 (10 = 3×3 + 1).
Question 5: Which MATLAB command is used to clear all variables from the workspace?
- delete all
- clear (Correct answer)
- clc
- reset
Correct answer: clear
The clear command removes all variables from the current workspace; clc only clears the Command Window text.
Question 6: How do you create a column vector of values from 1 to 10 with step 2 in MATLAB?
- (1:2:10)' (Correct answer)
- [1,3,5,7,9]
- linspace(1,10,2)'
- colon(1,10,2)'
Correct answer: (1:2:10)'
The expression (1:2:10)' creates the row vector [1 3 5 7 9] and transposes it into a column vector.
Question 7: What function would you use to find the maximum value in each column of a matrix M?
- max(M)
- max(M, [], 1) (Correct answer)
- max(M, [], 2)
- max(max(M))
Correct answer: max(M, [], 1)
max(M, [], 1) operates along dimension 1 (rows), returning the maximum of each column.
Which MATLAB function returns the size of each dimension of an array?