MATLAB Practice Test Video Answers

1. B
Row vectors in MATLAB are created using square brackets with elements separated by commas or spaces. Semicolons create column vectors, curly braces create cell arrays, and parentheses are used for indexing, not array creation.

2. B
The .* operator performs element-wise multiplication where corresponding elements are multiplied. The * operator without the dot performs standard matrix multiplication following linear algebra rules.

3. A
MATLAB uses (row, column) indexing. The notation A(2:4, 3) selects rows 2 through 4 from column 3, returning a 3×1 column vector containing three elements.

4. C
The size() function returns the dimensions of a matrix as [rows, columns]. The length() function returns only the largest dimension, while dim() and sizeof() are not standard MATLAB functions.

5. B
The ^ operator performs exponentiation in MATLAB. Therefore, 5^2 calculates 5 raised to the power of 2, which equals 25.

6. B
The command “clear all” removes all variables from the workspace memory. This is useful for starting fresh or freeing up memory during long computational sessions.

7. B
The colon operator with three arguments (start:step:end) creates a sequence. Here, 1:2:9 starts at 1, increments by 2, and stops at or before 9, producing [1, 3, 5, 7, 9].

8. D
Both inv(A) and A^(-1) compute the matrix inverse in MATLAB. The inv() function is the explicit inverse function, while A^(-1) uses matrix power operation to achieve the same result.

9. B
The eye(n) function creates an n×n identity matrix with ones on the main diagonal and zeros elsewhere. eye(3) creates a 3×3 identity matrix.

10. C
The semicolon (;) at the end of a statement suppresses the output display in the Command Window. This is useful for preventing clutter when executing many commands.

11. C
The zeros(m,n) function creates an m×n matrix filled with zeros. Therefore, zeros(2,3) creates a 2-row by 3-column matrix of zeros.

12. B
The eig() function computes eigenvalues and optionally eigenvectors of a square matrix. It is the standard MATLAB function for eigenvalue decomposition.

13. C
MATLAB FOR loops use the syntax “for variable = expression” where the expression typically uses the colon operator. The loop iterates through each value in the expression.

14. B
The numel() function returns the total number of elements in an array, regardless of its dimensions. It is equivalent to prod(size(A)) for any array A.

15. B
The single quote operator (‘) computes the conjugate transpose (Hermitian transpose) of a matrix. For real matrices, this is equivalent to the standard transpose.

16. C
The rand() function generates uniformly distributed random numbers between 0 and 1. The randn() function generates normally distributed random numbers with mean 0 and variance 1.

17. B
MATLAB functions are defined using the keyword “function” followed by output variables, the equals sign, function name, and input arguments in parentheses.

18. A
The forward slash (/) performs matrix right division, solving xA = B. The backslash (\) performs left division, solving Ax = B. The dot operators (./ and .\) perform element-wise division.

19. B
The linspace(a, b, n) function creates n linearly spaced points between a and b, inclusive. linspace(0, 10, 5) creates 5 points: [0, 2.5, 5, 7.5, 10].

20. B
The title() function adds a title string above the current axes in a figure. It accepts a string argument that becomes the plot title.

21. B
The mod(a, b) function returns the remainder after division of a by b. mod(17, 5) = 17 – 5*floor(17/5) = 17 – 5*3 = 17 – 15 = 2.

22. B
The horzcat() function concatenates arrays horizontally (along columns). This is equivalent to using square brackets: [A, B]. vertcat() or [A; B] concatenates vertically.

23. B
When the input is a vector, diag(v) creates a square diagonal matrix with the vector elements on the main diagonal. When the input is a matrix, it extracts the diagonal elements.

24. D
Both && and & represent logical AND in MATLAB. The && operator is short-circuit AND (used for scalars), while & performs element-wise AND and works on arrays.

25. B
The floor() function rounds toward negative infinity. For negative numbers, this means rounding away from zero. floor(-3.7) rounds down to -4.

26. D
Both the backslash operator (A\b) and linsolve(A, b) solve the linear system Ax = b. The backslash operator uses optimized algorithms based on matrix properties.

27. A
The reshape() function rearranges matrix elements into new dimensions. Since 3×4 = 12 = 2×6, the reshape is valid. Elements are taken column-wise from the original matrix.

28. B
The save() function saves workspace variables to a MAT-file. Using save(‘filename’) saves all variables; specific variables can be listed after the filename.

29. B
The strcmp() function performs case-sensitive string comparison. MATLAB’ and ‘matlab’ differ in case, so the function returns 0 (false). Use strcmpi() for case-insensitive comparison.

30. B
When called with two output arguments, max(v) returns both the maximum value and the index of its first occurrence. With one output, it returns only the maximum value.

31. B
The “hold on” command retains the current plot so that subsequent plotting commands add to the existing graph rather than replacing it. Use “hold off” to return to default behavior.

32. B
The det() function computes the determinant of a square matrix. The determinant is a scalar value that provides information about the matrix properties, including invertibility.

33. C
The sum(A, dim) function sums along the specified dimension. Dimension 2 represents columns, so sum(A, 2) computes the sum of each row, returning a column vector.

34. C
The break keyword immediately exits the innermost FOR or WHILE loop. Execution continues with the statement following the loop. The continue keyword skips to the next iteration instead.

35. B
The find() function returns the linear indices of nonzero elements. When combined with a logical expression like A > 5, it returns the indices where the condition is true, not the actual values.

36. B
Cell arrays in MATLAB are created using curly braces {}. Cell arrays can store data of different types and sizes in each cell, unlike regular arrays which require uniform data types.

37. A
The rank() function computes the rank of a matrix, which is the number of linearly independent rows or columns. It uses singular value decomposition to determine the rank numerically.

38. B
The “who” command displays a list of all variables currently stored in the MATLAB workspace. The “whos” command provides more detailed information including size, bytes, and class.

39. B
The struct() function creates a structure array in MATLAB. Structures allow you to group related data of different types using named fields, similar to records in other programming languages.

40. B
The ceil() function rounds toward positive infinity. For negative numbers, this means rounding toward zero. ceil(-2.3) rounds up to -2, which is closer to positive infinity than -3.