MATLAB Questions and Answers – Syntax Error – 1

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

1. What is the error in the following?

input(“pop”)

a) None
b) “”
c) Parentheses
d) No such command
View Answer

Answer: b
Explanation: The input to the input command should be within ‘’ and not “”. Double-inverted commas will lead to a syntactical error in MATLAB.
advertisement
advertisement

2. What is the error in the following code?

for i={1: 10}
p=a+1
end

a) None
b) {}
c) The : operator
d) Cannot be determined
View Answer

Answer: a
Explanation: MATLAB won’t give any error. but the loop will only run once because we have a syntactical error while defining the conditions for the loop.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What is the output of the following code?

advertisement
limit[1/x,0]

a) NaN
b) Inf
c) Syntactical Error
d) Logical error
View Answer

Answer: c
Explanation: The input to the limit command should be within parentheses. But here, the input is given within []. This leads to an error in MATLAB.
advertisement

4. What is the output of the following code?

length{ 1 2 }

a) Error due to space after 2
b) Error due to {}
c) Syntactical Error
d) 2
View Answer

Answer: c
Explanation: The input to the length function has to be within a parentheses. We don’t see the parentheses here, and we’ll find that this leads to an error in MATLAB.

5. What is the output of the following code?

limit((1 + 1/n)^n,n=Inf)

a) e
b) exp(1)
c) Syntactical Error
d) Cannot be determined
View Answer

Answer: c
Explanation: The correct syntax would be to simply state the limiting value as Inf. This is for new versions of MATLAB. The above code would result in a syntactical error.

6. What is the output of the following code?

A={1 2 3 4}
sum(A)

a) Error
b) 10
c) { 10 }
d) Logical Error
View Answer

Answer: a
Explanation: The sum function doesn’t work on cellular arrays. So the vector A is to be placed within [] to avoid this kind of error.

7. What is the output of the following code?

limit({sin(x)/x},x,Inf,right)

a) 0
b) Inf
c) Syntactical error
d) Error due to inconsistency in approaching limit
View Answer

Answer: c
Explanation: There is syntactical error in the above code snippet. The input, right, should be within a pair of single inverted commas. If that was so, there is still error due to inconsistency in approaching the limiting value because we cannot approach positive Infinity from it’s right hand side.

8. What is the output of the following code?

mean({1 2 3})

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

Answer: d
Explanation: Since the mean command uses the sum command, it also cannot accept cellular arrays as input since this input will go to the sum command first. Hence, the above code will give a syntactical error. Now, if we had given the input, within parentheses, within []- the output would’ve been 2 only.

9. What is the output of the following code?

det({1 2 3; 4 5 6; 7 8 9})

a) 0
b) Syntactical Error
c) A very small value
d) Cannot be determined
View Answer

Answer: b
Explanation: The input to the det command cannot be a cellular array. Since we have given the vector within {}, it results in an error. The output would have been very small value if the mtrix was given within [].

10. What is the output of the following code?

(1 2);

a) A cellular vector of dimension 1*2
b) A vector of dimension 1*2
c) A matrix of dimension 1*2
d) Error
View Answer

Answer: d
Explanation: To create a vector, one needs to give the elements within [] and not within parentheses. Hence, the above code will result in a syntactical error.

11. What is the output of the following code?

p=[1 3 2]; roots[p];

a) Syntactical Error
b) Undefined roots
c) -2,-1
d) Cannot be determined
View Answer

Answer: a
Explanation: The input to the roots command has to be within parentheses. Here, it’s given within [] so it’ll result in a syntactical error. If the input was given within parentheses, the output would’ve been -2,-1.

12. What is the output of the following code?

poly[ 1 2 3]

a) x + 2x + 3x2
b) x3 + 2x2 + x
c) Error
d) Cannot be determined
View Answer

Answer: c
Explanation: The input to the poly command has to be within parentheses. If the input is a vector, one can define the vector, prior to giving it as an input, or give the input like poly([1 2 3]).

13. What is the output of the following code?

polyint[{1 2 3}]

a) Syntactical error due to []
b) Syntactical Error due to {}
c) Logical Error
d) [1/2,1,3]
View Answer

Answer: b
Explanation: The answer would’ve been d. But the input to the polyint command is a cellular array and this results in a syntactical error. But firstly, the input to the polyint command should be within parentheses. But here, the input is within [] which is the first error MATLAB finds.

14. What is the output of the following command?

>> x={1 2 3};
>> t=[1 2 3];
>> plot(x,t)

a) plots a ramp function
b) plots r(t)-r(t-3)
c) Error due to plot
d) Error due to x
View Answer

Answer: d
Explanation: In the above code, x is a cellular array. It will create an error in the working of the plot command since the plot command cannot plot values in cellular arrays. Hence, there is a syntactical error while defining x for the plot command.

15. The input to the polar command is not necessary to be within parentheses.
a) True
b) False
View Answer

Answer: a
Explanation: The polar command is pre-defined in MATLAB and it takes inputs within parenthesis. In general, function syntax is the standard way to call functions in MATLAB. However, if we use command syntax, then the polar command can be invoked without using parenthesis. Hence, the above statement is true.

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.