MATLAB General MCQ 4 — Questions and Answers
Question 1: What is the MATLAB syntax to access elements in a struct field named 'age' of variable 'person'?
- person['age']
- person(age)
- person.age (Correct answer)
- getfield(age, person)
Correct answer: person.age
MATLAB uses dot notation to access struct fields, so person.age retrieves the 'age' field of the struct 'person'.
Question 2: Which MATLAB function is used to solve a system of linear equations Ax = b?
- solve(A, b)
- A \ b (Correct answer)
- inv(A) .* b
- lsq(A, b)
Correct answer: A \ b
The backslash operator A\b computes the solution to the linear system Ax=b efficiently without explicitly computing the inverse.
Question 3: What does the function cell2mat do in MATLAB?
- Converts a matrix to a cell array
- Converts a cell array of matrices into a single matrix (Correct answer)
- Creates a matrix from a cell index
- Checks if a cell contains matrix data
Correct answer: Converts a cell array of matrices into a single matrix
cell2mat concatenates the contents of a cell array of numeric arrays into a single numeric matrix.
Question 4: In MATLAB, which loop type checks the condition AFTER executing the loop body at least once?
- for loop
- while loop
- do-while loop
- MATLAB has no such loop (Correct answer)
Correct answer: MATLAB has no such loop
MATLAB does not have a built-in do-while loop; both for and while loops check their conditions before each iteration.
Question 5: What is the purpose of the 'nargin' variable inside a MATLAB function?
- Number of output arguments requested
- Number of input arguments passed to the function (Correct answer)
- Name of the input argument
- A flag for argument validation
Correct answer: Number of input arguments passed to the function
nargin returns the number of input arguments provided by the caller, allowing functions to handle optional inputs.
Question 6: Which MATLAB function applies a given function to each element of an array and returns the results?
- cellfun
- arrayfun (Correct answer)
- structfun
- mapfun
Correct answer: arrayfun
arrayfun applies a function to each element of an array and collects the results into an output array of the same size.
Question 7: What does the MATLAB function 'reshape(A, m, n)' do?
- Resizes matrix A to m×n by padding with zeros
- Rearranges elements of A into an m×n matrix without changing data (Correct answer)
- Sorts A into an m×n grid
- Crops A to m rows and n columns
Correct answer: Rearranges elements of A into an m×n matrix without changing data
reshape rearranges the elements of A column-by-column into a new matrix of size m×n; the total number of elements must remain the same.
What is the MATLAB syntax to access elements in a struct field named 'age' of variable 'person'?