MATLAB Questions and Answers – Wrong or Unexpected Output

This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Wrong or Unexpected Output”.

1. What is the output of the following code?

cos(pi/2)

a) 0
b) Close to 0
c) less than 0
d) Undefined
View Answer

Answer: b
Explanation: We’ve not declared pi as symbolic. The value of pi stored in MATLAB results in a calculation of cos(pi/2) by approximation and hence, the value evaluated will be significantly close to 0 but not exactly 0. Hence, we should be careful while evaluating the values of such trigonometrical functions.
advertisement
advertisement

2. What is the output of the following code?

sprintf(5%”)

a) “5”
b) “5%”
c) 5
d) 5%
View Answer

Answer: a
Explanation: To print the % as a character, we need to use %% within the sprintf function. Here, we haven’t used it twice so only 5 would get printed.
Note: Join free Sanfoundry classes at Telegram or Youtube

3. Why isn’t the answer 10.230?

advertisement
>> sprintf("%1.3d", 10.23)
 ans = 
       "1.023e+01"

a) We’ve used %d
b) We’ve used “”
c) Character arrays are like this
d) Undefined answer
View Answer

Answer: a
Explanation: If we had used %f, the answer would’ve been 10.230 since the number gets defined as a floating point number. But here, %d defines the number as an integer so the output becomes different.
advertisement

4. How many times will the loop run?

for i={1:.1:5}
p=p+1;
end

a) 50
b) Infinite
c) 0
d) 1
View Answer

Answer: d
Explanation: The control will exit the loop after the first iteration only so the loop never runs. This is because p is not defined previously or inside the loop.

5. What is the output of the following code?

A=[1 2 3 0; 4 5 6 8; 1 2 3 4; 1 3 4 5 ; 4 0 0 0 ];
length(A)

a) 5
b). 4
c) 5*4
d) 4*5
View Answer

Answer: a
Explanation: The length function would return the highest length it finds in the input vector. Since, the input vector has 5 columns and 4 rows, it will return 5.

6. What will be the result after we press Return?

>> input('Ro !')
Ro ! Ho !

a) ans= Ho !
b) Error
c) No such command named input
d) Cannot be determined
View Answer

Answer: b
Explanation: The input() command only takes values as input i.e. it won’t take characters as input. Hence, when we press Return, there will be an error.

7. A student wants to find the following limit
The entire function is to be divided by x2 but according to the code in figure but he writes the following code
limit(1-cos(2*x)/x^2,x,0)
What will he get?
a) a2/2!
b) a2/2
c) Error
d) -Inf
View Answer

Answer: d
Explanation: He has written it wrong. The entire function is to be divided by x2 but according to his code, only cos 2x gets divided by x2 and hence it yields -Inf.

8. How long will ‘Hola !’ get printed?

>>input(‘Hola !’)
Hola !

a) Until a value is given as an input
b) Until a character is given as an input
c) Until anything is given as an input
d) Once
View Answer

Answer: a
Explanation: The input command really starts an infinite loop. This means that until we give a value as an input, it’ll keep on printing ‘Hola !’- it’ll give an error if we give a character as an input.

9. What is the output of the following code?

syms x
limit(1/-x,x,-0)

a) NaN
b) Inf
c) 0
d) Syntactical error
View Answer

Answer: a
Explanation: The bidirectional limit does not exist at x=0. It should not be expected that the answer is Infinite because the limit is infinite if we approach x=0 from left or right while we say that it does not exist at x=0.

10. Why did the following happen?

>> p
p = 
    "10"
>> p+1
ans = 
     "101"

a) p is a character array
b) p is binary
c) Error
d) Cannot be determined
View Answer

Answer: a
Explanation: It’s seen that p=”10” which signifies that p is a character array only. Hence, adding 1 to it is merely adding another element into the character array so one shouldn’t expect it to be 11.

11. Why di the following happen?

>> sprintf("   %   ")
ans = 
     "  "

a) Error
b) % is not recognized
c) % is an operator
d) Cannot be determined
View Answer

Answer: b
Explanation: To print a ‘%’, we need to put ’%%’ within the double quotes while in the above case, we observe ‘%’. This is not recognized by the sprintf command and so, the output is a blank character array.

12. What is the output of the following code?

det([1 2 3; 4 5 6; 7 8 9])

a) Error
b). 0
c) -0
d) A very small value
View Answer

Answer: d
Explanation: Any determinant in MATLAB is calculated using the LU decomposition method which eventually results in rounding off errors in case of singular matrices. Since the given matrix is singular, it will give a very small value.

13. What is the output of the following code?

 >> { 1 2}
 ans = 
   1*2 cell array
     [1]   [2]
>> ans+{3 4}

a) { 1 2 3 4}
b)

     { 1 2
       3 4}

c) { 4 6}
d) Error
View Answer

Answer: d
Explanation: Cellular arrays cannot be added in MATLAB. Hence the above code will result in an error. if the arrays were within [], the would’ve been simple 1d vectors which could be easily added.

14. What is the output of the following code?

tf2zp({1 2}, {2 1})

a) 0
b) Returns the zeros and poles of the transfer function (s+2)/(2s+1)
c) Syntactical Error
d) Logical Error
View Answer

Answer: c
Explanation: The input to the tf2zp command cannot be cellular arrays. But we’ve used {} in the above code and this leads to a syntactical error in MATLAB.

15. What is the output of the following code?

syms ‘t’,’p’;

a) Both t and p are defined as symbolic
b) Only t gets defined as symbolic
c) Only p gets defined as symbolic
d) Nothing happens
View Answer

Answer: b
Explanation: There is a syntactical error in using syms. Only t gets defined while p gets initialized to the ans variable so it’s not defined as symbolic. This is due to the ‘ , ‘ present which leads to this error, we must remove them, to declare multiple characters as symbolic variables.

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.