MATLAB Multiple Choice Questions on “Errors in Input – 1”.
1. What will be the output of the following code?
a) A=-1
b) Undefined function or variable ‘cod’
c) Undefined function or variable ‘sim’
d) Undefined function or variable ‘sim’ and ‘cod’
Answer: c
Clarification: ‘sin’ and ‘cod’ functions have been written in place of ‘sine’ and ‘cos’ functions. If there are more than 1 error in a line of code, MATLAB will return only the first error it comes across. It will neglect the rest of the code. So it ignores the error ‘cod’.
2. What is the output of the following code?
a) [1 4 9]
b) A= 1 4 9
c) A= [1, 4, 9]
d) Inputs must be a scalar or a square matrix
Answer: d
Clarification: Multiplying a column or row vector with a scalar or another vector requires the code to be “A.^2” in place of “A^2”. If this line is replaced, the output will be: A=1 4 9
Output: Inputs must be a scalar and a square matrix.
To compute element wise POWER, use POWER (.^) instead.
3. What is the output of the following code?
a)
A = 2 2
2 2
b)
A = 1 1
1 1
c) Error using ^ To compute element wise POWER, use POWER (.^) instead
d) No output
Answer: a
Clarification: ‘A^2’ implies we are multiplying A with A, A being a square matrix. So the answer follows. If the line of code was ‘A.^2’, the answer would be a square matrix, such that aij=2 of order 2*2.
Output: Cursor shifts to the next line, waiting for a new line of code.
4. What is the output of the following code?
a) c= [1 8]
b)
c = 1
8
c) Inner Matrix dimensions must agree
d) No output since we have closed the line of code with a semicolon
Answer:c
Clarification: Multiplication of two row vectors is possible if we use the ‘.*’ operator. If B was a 1*2 column vector, the multiplication would have been possible resulting in a multiplication of vectors and generating c=9 as output.
5. What is the output of the following line of code?
a) No output as we’ve closed the line of code with a semi-colon
b)
1 2 3 4 5 6 7 8 9 0 1.5708 3.1416 4.7124 6.2832 7.8540 9.4248 10.9956 12.5664
c) Error: Unexpected MATLAB expression
d) Undefined function or variable ‘pi’
Answer: c
Clarification: Multiplying an integer with a variable which contains a constant value requires the use of ‘*’. So 4pi should be ‘4*pi’. ‘pi’ is predefined in MATLAB as 3.1416.
6. What is the error in the code?
a) Third brackets are wrong
b) The semicolon within the second third brackets
c) There is no error
d) Error: Expression or statement is incorrect–possibly unbalanced
Answer: d
Clarification: A matrix cannot have parentheses within itself. Any integer, whether to be included row wise or column wise, is to be written within a third bracket.
7. What is the output in the following code?
a=[[1;22],[53;9],[13;2]];
a) There is no output
b) Columns are to be introduced by placing semi-columns
c) Dimensions of matrices being concatenated are not consistent
d)
a = 1 53 13 22 9 2
View Answer
Clarification: The a matrix will be stored in the workspace as
a = 1 53 13 22 9 2
There is no error and there will be no output since the dimensions of matrices being concatenated are constant.
8. What is the difference between the two codes?
a> P=[91,’pi’]; b> Q=[91,pi];
a) Code a initialises P as a character array while code b initialises Q as an integer array
b) Both P and Q will be integer arrays
c) Code b initialises P as a character array while code a initialises Q as an integer array
d) Both P and Q will be character arrays
Answer: a
Clarification: One should keep in mind that once while declaring a vector, a character is introduced with a pair of single-inverted commas, the vector becomes a character array. So it will show different answers while doing algebraic operations on it.
Output:
For a> P= [pi
b> Q= 91 3.1416
9. What is the output of the following code?
a) Inf
b) P = Inf
c) P = -1.9952
d) Undefined function or variable ‘tan90’
Answer: d
Clarification: To evaluate numerical value of any trigonometric expression, the angles should be placed within a pair of parentheses. That’s why the above code generates an output.
10. What is the output for the following code?
a) No output
b) Never will there be an output
c) a, b, p are not initialized
d) p=9
Answer: b
Clarification: Any control structure or loop structure has to be terminated with the code ‘end’. Else the cursor will keep on demanding further lines of codes. This is why, for the above code, the output will never appear.
11. What is the output of the following code?
a) P = 1
b) P = .8932
c) P = .99999
d) Error
