MATLAB Questions and Answers – Errors in Input – 1

This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Errors in Input – 1”.

1. What will be the output of the following code?

A=sim(pi)+cod(pi)

a) A=-1
b) Undefined function or variable ‘cod’
c) Undefined function or variable ‘sim’
d) Undefined function or variable ‘sim’ and ‘cod’
View Answer

Answer: c
Explanation: ‘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’.
advertisement
advertisement

2. What is the output of the following code?

A=[1 2 3]; A^2;

a) [1 4 9]
b) A= 1 4 9
c) A= [1, 4, 9]
d) Inputs must be a scalar or a square matrix
View Answer

Answer: d
Explanation: 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?

advertisement
A=[1 1; 1 1]; A^2

a)

A = 2 2
    2 2
advertisement

b)

A = 1 1
    1 1

c) Error using ^ To compute element wise POWER, use POWER (.^) instead
d) No output
View Answer

Answer: a
Explanation: ‘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=[1 2]; B=[1 4]; c=A*B;

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
View Answer

Answer:c
Explanation: 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?

			t=0:pi/8:4pi;

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’
View Answer

Answer: c
Explanation: 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=[[1;2];(2,3)]

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
View Answer

Answer: d
Explanation: 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
Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: 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?

P=tan90

a) Inf
b) P = Inf
c) P = -1.9952
d) Undefined function or variable ‘tan90’
View Answer

Answer: d
Explanation: 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?

if(a>b) p=9;

a) No output
b) Never will there be an output
c) a, b, p are not initialized
d) p=9
View Answer

Answer: b
Explanation: 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?

P=sin[90];

a) P = 1
b) P = .8932
c) P = .99999
d) Error
View Answer

Answer: d
Explanation: The input to the sin command has to be within parentheses. Since the input is given within [], it leads to an error.

12. What is the output of the following code?

system(cmd)

a) Opens command prompt
b) Opens command prompt in a separate window
c) Opens command prompt in MATLAB
d) Error
View Answer

Answer: d
Explanation: The input to the system command has to be within ‘’. Since, the cmd command has been written without it, it leads to an error.

13. Why is the output, as shown, ‘poe’?

>>clipboard(‘paste’, ‘Do re Mi fa’)
ans =
      ‘poe’

a) ‘poe’ was initially in the clipboard
b) Cannot be determined
c) Error
d) The text gets changed
View Answer

Answer: a
Explanation: If we’ve used the clipboard command to copy a string before, we need to paste it already. We cannot use the ‘paste’ keyword to paste a new string if there’s already a string in our clipboard.

14. What is the output of the following code?

clipboard('cut','Do re Mi fa')

a) Error due to syntax
b) Error due to command
c) Error due to cut
d) Cuts the portion of a text where ‘Do re Mi fa’ is written
View Answer

Answer: c
Explanation: The clipboard command allows the user to only copy and paste. Hence, we get an error. It can’t be used to cut a portion of a text.

Sanfoundry Global Education & Learning Series – MATLAB.

To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.