MATLAB Interview Questions and Answers for freshers on “Vectors and Matrices – 2”.
1. What is the output of the following code?
A=[1 2 3..
];
a) The output is suppressed
b) A row vector
c) A row vector concatenated with a null matrix
d) Error
Answer: d
Clarification: The above code will give in an error due to the fact that ellipsis hasn’t been done properly. There should’ve been a semi-colon after 3 and we need to give 3 dots. Thereafter, option “the output is suppressed” is actually correct because the output would essentially be suppressed while a row vector A gets stored in the workspace.
2. What is the output of the following code?
A=[1 2 a..;
];
a) Error due to a
b) Error due to A
c) Error due to [];
d) Error due to ;
Answer: a
Clarification: Typically, the row vector A contains integers as the first element and thus the entire array becomes an integer vector. But the inclusion of a results in an error since a is a character. If, however, a was written within ‘’, there wouldn’t have been a error.
3. What is the output of the following code?
A=[1 2 ‘a’;…
];
a) Error due to ‘a’
b) Error due to ‘…’
c) Error due to []
d) ‘a’
Answer: d
Clarification: MATLAB would show such kind of an output since the A vector gets defined as a character array when a gets defined within ‘’. The syntax for ellipsis is correct and there is no error.
Output: ‘a’
4. What is the output of the following code?
A=[1 2 ‘a’;… ‘f’ ‘q’ ‘w’];
a) A 2*3 character array
b) A 3*2 character matrix
c) A 3*2 character vector
d) A 2*3 character vector
Answer: b
Clarification: The above code performs ellipsis to concatenate two matrices vertically. Thus, the output is a 2*3 matrix since the dimension of each vector, it concatenates, is 3. Now, the presence of a character makes the vector character vectors.
5. What is the output of the following code?
A=[1 2 ‘a’;… ‘f’ ‘q’ ‘w’;… ]];
a) Syntax Error
b) A 2*3 character matrix
c) The concatenation of 2 vectors, vertically, with size 3
d) Cannot be determined
Answer: a
Clarification: There are two ]] at the end. This leads to an error. This is because only one ] was sufficient to produce A as defined in the option concatenation of 2 vectors, vertically, with size 3. But the usage of an extra ] leads to an error the code.
6. What is the output of the following code?
A=[1 2 ‘a’…;
‘a’ ‘b’ ‘c’;…
];
a) Error in syntax
b) A 3*2 matrix of characters
c) Error due to 1 and 2
d) Error due to a
Answer: a
Clarification: Ellipsis has been written wrong in the first line. This is because there has to be 4 ‘.’ for concatenating vectors row-wise. Since we’ve not used 4 dots, it leads to an error.
7. What is the output of the following code?
A=[1 2 ‘a’….;
‘a’ ‘b’ ‘c’;…
];
a) A 1*6 matrix
b) A 6*1 matrix
c) Error in the code
d) Error in ellipsis
Answer: a
Clarification: In the above code, ellipsis is done to concatenate two vectors row-wise. There is no error in the syntax ad the output A 1*6 matrix.
8. What is the output of the following code?
clear ALL
a) Clears the workspace
b) Clears the matrices
c) Clears the vectors
d) Clears ALL
