Matlab Arrays vs. Matrices Quiz

Matlab
0 Passed
0% acceptance

35 questions exploring the core data structures in MATLAB, focusing on dimensionality, indexing, and manipulation techniques.

35 Questions
~70 minutes
1

Question 1

In MATLAB, what is the technical definition of a single scalar number like `5`?

A
It is a 1x1 matrix
B
It is a primitive integer type
C
It is a 0-dimensional object
D
It is a pointer
2

Question 2

You have a matrix `M` of size 3x3. You wish to access the element located in the second row and third column. Which command achieves this?

A
val = M(2, 3);
B
val = M[2, 3];
C
val = M(3, 2);
D
val = M{2, 3};
3

Question 3

Consider the code above. `A` is a 2x3 matrix. After reshaping it into a 3x2 matrix `B`, what value will be at `B(2, 1)`?

matlab
A = [1 2 3; 4 5 6];
B = reshape(A, 3, 2);
A
4
B
2
C
5
D
3
4

Question 4

What is the primary difference between a row vector and a column vector?

A
A row vector is 1xN; a column vector is Nx1
B
A row vector is Nx1; a column vector is 1xN
C
Row vectors can only hold integers
D
There is no functional difference
5

Question 5

You initialize a vector `v` with three elements. You then assign a value to the 5th index. What happens to the 4th element?

matlab
v = [10, 20, 30];
v(5) = 50;
A
It is automatically filled with 0
B
It becomes NaN
C
It remains undefined (error)
D
It copies the value from index 3
6

Question 6

Which syntax correctly concatenates two row vectors `A` and `B` horizontally (end-to-end)?

A
C = [A, B]
B
C = [A; B]
C
C = A + B
D
C = merge(A, B)
7

Question 7

You are using linear indexing on a 2x2 matrix `M`. Which element does `M(3)` retrieve?

matlab
M = [1 2; 3 4];
val = M(3);
A
The element at row 1, column 2 (value 2)
B
The element at row 2, column 1 (value 3)
C
The element at row 2, column 2 (value 4)
D
Error: Index out of bounds
8

Question 8

When reshaping a matrix, what constraint must be satisfied regarding the number of elements?

A
The total number of elements must remain constant
B
You can add zeros if the new size is larger
C
You can truncate data if the new size is smaller
D
The matrix must be square
9

Question 9

Given two row vectors `A` and `B`, you execute the code above. What are the dimensions of the resulting matrix `C`?

matlab
A = [1 2 3];
B = [4 5 6];
C = [A; B];
A
2x3 (2 rows, 3 columns)
B
3x2 (3 rows, 2 columns)
C
1x6 (1 row, 6 columns)
D
6x1 (6 rows, 1 column)
10

Question 10

What does the keyword `end` represent when used inside an indexing expression like `v(end)`?

A
The index of the last element in that dimension
B
The value of the last element
C
It terminates the script
D
It selects the entire array
11

Question 11

You create a matrix using the command `zeros(2, 4)`. How many total elements does this matrix contain?

matlab
M = zeros(2, 4);
A
8
B
6
C
2
D
4
12

Question 12

Which command would you use to find the dimensions (rows and columns) of a matrix `A`?

A
size(A)
B
length(A)
C
dim(A)
D
count(A)
13

Question 13

What is the effect of assigning an empty matrix `[]` to the second column of `A`?

matlab
A = [10 20; 30 40];
A(:, 2) = [];
A
It deletes the second column, leaving a 2x1 matrix
B
It sets all values in the second column to 0
C
It sets all values in the second column to NaN
D
Error: Cannot assign empty to a range
14

Question 14

If you try to vertically concatenate matrices `A` and `B` using `[A; B]`, what condition must be met?

A
They must have the same number of columns
B
They must have the same number of rows
C
They must have the exact same dimensions
D
One must be a scalar
15

Question 15

You extract a sub-matrix `sub` from `M` using the indices `1:2` for rows and `2:3` for columns. What does `sub` look like?

matlab
M = [1 2 3; 4 5 6; 7 8 9];
sub = M(1:2, 2:3);
A
[2 3; 5 6]
B
[1 2; 4 5]
C
[5 6; 8 9]
D
[2 3; 8 9]
16

Question 16

What is the result of `length(M)` if `M` is a 4x10 matrix?

A
10
B
4
C
40
D
14
17

Question 17

This question tests matrix multiplication rules, which depend on dimensions. `A` is 1x2. `B` is 2x1. What is the size of the result `C`?

matlab
A = [1 2];
B = [3; 4];
C = A * B;
A
1x1 (Scalar)
B
2x2 Matrix
C
1x2 Vector
D
Error
18

Question 18

How does MATLAB store a 2D matrix in memory?

A
Column-major order (columns are contiguous)
B
Row-major order (rows are contiguous)
C
As a linked list of rows
D
Randomly
19

Question 19

You use the index vector `[1 1 1]` to access elements of `v`. What does `res` contain?

matlab
v = 1:5;
idx = [1 1 1];
res = v(idx);
A
[1 1 1]
B
1
C
Error: Duplicate index
D
[1 2 3]
20

Question 20

Which function creates an identity matrix (ones on diagonal, zeros elsewhere)?

A
eye
B
identity
C
ones
D
diag
21

Question 21

You have a vector `A = [1 2 3]`. You delete the second element. What is the new value of `A`?

matlab
A = [1 2 3];
A(2) = [];
A
[1 3]
B
[1 0 3]
C
[1 NaN 3]
D
[1 2]
22

Question 22

What is a 'scalar' in the context of MATLAB array operations?

A
A single value that can operate on every element of an array simultaneously
B
A variable that cannot change
C
A text string
D
A 1D vector
23

Question 23

The `magic(3)` function creates a 3x3 matrix. You use `M(:, end)` to create `last_col`. What does the colon `:` signify here?

matlab
M = magic(3);
last_col = M(:, end);
A
Select all rows
B
Select all columns
C
Select the diagonal
D
Select nothing
24

Question 24

If you assign `A(5,5) = 1` to a variable `A` that doesn't exist yet, what does MATLAB do?

A
Creates a 5x5 matrix with 1 at (5,5) and zeros elsewhere
B
Errors because A is undefined
C
Creates a scalar A = 1
D
Prompts the user for dimensions
25

Question 25

You use `cat(3, A, B)` to concatenate two 1x2 vectors along the 3rd dimension. What is the size of `C`?

matlab
A = [1 2];
B = [3 4];
C = cat(3, A, B);
A
1x2x2
B
1x4
C
2x2
D
3x2
26

Question 26

Which operator is used to transpose a real matrix (swap rows and columns)?

A
'
B
^T
C
.~
D
#
27

Question 27

`A` is a column vector. You apply the transpose operator `.'`. What is `B`?

matlab
A = [10; 20; 30];
B = A.';
A
A row vector [10 20 30]
B
A column vector [10; 20; 30]
C
A 3x3 matrix
D
Scalar 60
28

Question 28

What is the result of `numel(zeros(3, 4))`?

A
12
B
7
C
3
D
4
29

Question 29

You use `reshape(A, [], 2)`. What does the empty bracket `[]` tell MATLAB to do?

matlab
A = [1 2 3 4];
B = reshape(A, [], 2);
A
Automatically calculate the number of rows
B
Leave the rows empty
C
Use 0 rows
D
Error
30

Question 30

Can a matrix contain different data types (e.g., numbers and strings mixed)?

A
No, standard matrices must be homogeneous
B
Yes, always
C
Yes, if declared as 'mixed'
D
Only if they are integers
31

Question 31

The `sub2ind` function converts row/col subscripts to a linear index. For a 2x2 matrix, what is the linear index of row 2, column 1?

matlab
A = [1 2; 3 4];
idx = sub2ind(size(A), 2, 1);
A
2
B
3
C
1
D
4
32

Question 32

What happens if you index with a logical mask that is the same size as the matrix?

A
It returns a column vector of values where the mask is true
B
It returns a matrix of the same size with zeros where false
C
It returns the indices
D
Error
33

Question 33

This is an advanced broadcasting question. `A` is 1x3. `B` is 3x1. What is the size of `A + B` in modern MATLAB?

matlab
A = [1 2 3];
B = [1; 2; 3];
C = A + B;
A
3x3
B
Error: Dimensions mismatch
C
1x3
D
3x1
34

Question 34

Which function flips a matrix upside down (reverses the order of rows)?

A
flipud
B
fliplr
C
reverse
D
invert
35

Question 35

You assign 10 to `A(:)`. What is the result?

matlab
A = [1 2; 3 4];
A(:) = 10;
A
A becomes a 2x2 matrix of all 10s
B
A becomes a column vector of 10s
C
A becomes a scalar 10
D
Error

QUIZZES IN Matlab