This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “More about Loops – 2”.
1. The for loop performs at least ___ iteration/s.
a) 1
b) not necessarily an iteration
c) 2
d) Error
View Answer
Explanation: Suppose the condition given to check by the for loop has a logical error- the for loop won’t iterate even once. Hence, it is not necessary that the for loop will run even once.
2. Multiple graphs can be plotted, in the same window, if we use the ___ command in a loop.
a) hold on
b) held on
c) hold off
d) not possible
View Answer
Explanation: The hold on command can be used to plot multiple graphs in the same window by using a loop. Hold off is used to exit the functioning of the hold on command. Hence, the correct option is hold on.
3. The number of iterations in the following loop is
p=3;for i=(3:4) p=p+2; end
a) 0
b) 1
c) 3
d) 4
View Answer
Explanation: If the counter value is <= the final value, the loop will perform an iteration. Hence, the above loop iterates twice to yield p=6. There is no error.
4. What is the output of the following code?
q=0;for i=(1:1) q=q+5; end
a) Output is suppressed
b) Syntactical error
c) q=5
d) Declaration error
View Answer
Explanation: The output is suppressed due to the semi-colon after q=q+5. The output of the following code would’ve only been q=5 if the output was not suppressed. There is no error in the above code.
5. What is the output of the following code?
a=0;for i=[1:1] q=a-55; end
a) Syntactical Error
b) Declaration error
c) Output is suppressed
d) q=-55
View Answer
Explanation: The expression for counter can be given within [] in the for statement. The output is suppressed due to the semi-colon after q=a-55. The output of the following code would’ve only been q=-55 if the output was not suppressed. There is no error in the above code.
6. What is the nature of the error in the following statement?
for i=1:
a) Incomplete
b) Unexpected operator
c) Invalid keyword
d) No error
View Answer
Explanation: MATLAB would recognize that the above statement for starting the for loop is incomplete. Hence, it will give the option incomplete as an error.
7. What is the nature of the error in the following statement?
for i=:1
a) Incomplete syntax
b) Unexpected MATLAB operator
c) No such keyword
d) No error
View Answer
Explanation: The syntax of the for loop is not recognized by MATLAB. Hence, it will give the unexpected MATLAB operator as an output.
8. What is the output of the following code?
i=0; while[i<5] i=i+1; end
a) Syntactical Error
b) Declaration Error
c) Output is suppressed
d) i=5
View Answer
Explanation: There is no error due to []. The output is suppressed due to the ‘ ; ‘. The value of I stored in the workplace is i=5.
9. The condition of a while loop cannot be given in ________
a) ()
b) []
c) {}
d) <>
View Answer
Explanation: The variables of the condition gets converted to a cell format if the condition is given in {}. Thereafter, the variables cannot be converted into a logical form. It can be given within () and [].
10. What is the output of the following code?
while{i<5} i=i+1234; end
a) i=1234
b) Output is suppressed
c) Error in converting the cell to logical form
d) Error in i
View Answer
Explanation: MATLAB returns the first error it finds. Now, i is not defined before using it as a variable for a condition in the while loop. But the first error is the fact that the condition is given in {} so it is in a cell format and the conversion to logical form is not possible. Hence, MATLAB will return an error.
11. What is the output of the following code?
i=0;while(i<5) i=i-1; end
a) Error
b) i=-Inf
c) i=NaN
d) Infinite loop
View Answer
Explanation: The following code will start an infinite loop. The control can be broken form the loop by pressing ctrl+c.
Sanfoundry Global Education & Learning Series – MATLAB.
To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers.