MATLAB Questions and Answers – Syntax Error – 2

This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Syntax Error – 2”.

1. What is the output of the following code?

for ( i=1:10 )
if{i<3}
break
end
end

a) Syntactical Error
b) Logical Error
c) i=1
d) i=2
View Answer

Answer: a
Explanation: There is a syntactical error in the above code while giving the condition for the if structure. Hence, this leads to an error in MATLAB. If the condition was given within parentheses, the output would’ve been option i=1.
advertisement
advertisement

2. What is the final value of i?

for i=[1:10]
end

a) 0
b) 1
c) Error
d) 10
View Answer

Answer: c
Explanation: The assignment of the index value is done within []. This leads to a syntactical error. We can put it in parentheses or just write it after for then the output would’ve been 10.

3. What is the output of the following code?

advertisement
syms p q;
x=p+2q;
findsym[x]

a) Syntactical Error
b) p,q
c) No such command
d) p
View Answer

Answer: a
Explanation: The findsym command takes input within parentheses. But here we have used the [] which leads to a syntactical error. The output would’ve been p,q if the input, x, was given within parentheses.
advertisement

4. What is the output of the following code?

syms p q;
x=p+2*q;
diff[x];

a) 1
b) 0
c) Syntactical Error
d) Logical Error
View Answer

Answer: c
Explanation: The output would’ve been 1 if the input to the diff command was given within parentheses. But the input has been given within [] which results in a syntactical Error.

5. What is the output of the following code?

syms pi;
p=2sin[pi]/cos[pi] + tan[3pi];

a) 0
b) A value very close to 0
c) Syntactical Error
d) Error in the expression
View Answer

Answer: d
Explanation: There would’ve been a syntactical error but we have not put a * sign between 2 and sin[2*pi]. This is the first error MATLAB finds and returns this as an error. There is also syntactical error since the input to the sin, cos or tan command should always be within parentheses only.

6. What is the output of the following code?

solve[‘x^2+1=0]

a) x=j,-j
b) x=+i,-i
c) Syntactical Error
d) Logical Error
View Answer

Answer: c
Explanation: The input to the solve command should always be within parentheses. Since, here, we’ve given the input within [], this will result in a syntactical error. The output would’ve been x=+i,-i if the input was given in parentheses.

7. What is the output of the following code?

syms [p,q]

a) Establishes p and q as symbolic variables
b) Syntactical Error
c) Logical Error
d) Only p is symbolically defined
View Answer

Answer: b
Explanation: We cannot declare vectors as symbolic. If we need to declare multiple variables as symbolic, we can write each variable with space in between like- syms p q and the output would’ve been establishes p and q as symbolic variables.

8. Vectors defined by [] can be cellular vectors.
a) True
b) False
View Answer

Answer: b
Explanation: Cellular vectors can only be defined by using a {}. If they are defined using [], it becomes a 1-D vector which is different from cellular arrays.

9. 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.

10. What is the output of the following code?

error{404}

a) 404
b) Error in the command
c) Error 404
d) Syntactical Error
View Answer

Answer: d
Explanation: The syntactical error in the above code is the fact that the input to the command is given within {}. It should be given within parentheses- then the output would’ve been option error in the command only.

11. What is the output of the following code?

warning(“a”)

a) Warning: a
b) Error in the code
c) Syntactical Error
d) Logical Error
View Answer

Answer: c
Explanation: There is a syntactical error in the above code. The input should be present within a pair of single inverted commas- to signify the warning message as a string. The output would’ve been option Warning: a in that case.

12. 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 the, to declare multiple characters as symbolic variables.

13. What is the output of the following code?

poly{1,2}

a) Syntactical Error
b) 1 3 2
c) Cannot be determined
d) 0
View Answer

Answer: b
Explanation: The poly command returns the co-efficients of the polynomial according to the given input, the inputs being the roots of the command. The output would’ve been 1 3 2 but the input is given within {} which leads to a syntactical error. It should be provided within parentheses.

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.