This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “More about Loops – 1”.
1. What is the output of the following code?
i=0;for i=0:2 i=1; end
a) Output is suppressed
b) Output shows 1 3 times
c) Output shows 1 2 times
Explanation: The output won’t be shown since we’ve added a semi-colon after the declaration of the variable i. If there was no semi-colon, the output would’ve been i=1, 3 times. There is no error.
2. What is the output of the following code?
i=0;for j=0:2 j=j-1 end
a) Infinite loop
c) Output is suppressed
Explanation: The output is suppressed since there is a semi-colon after the decrement of j. The counter variable is really treated as a different variable hence the value of j at the end of the loop is not 2.
3. What is the output of the following code?
a=0;for j=0:2 j=j+1 end
a) Syntactical Error
d) Keyword Error
Explanation: The end is a reserved keyword and it cannot be used in the same line with j+1. Hence, the for-loop will end without even beginning. The output would’ve been 3 if the end was in the next line. There is no syntactical error.
4. What is the final value of j from the following code?
for j=(0:3) j=j+1; end
a) Syntactical error
b) Declaration error
Explanation: The syntax of for loop in MATLAB does not include parentheses. Here, the counter is introduced within parentheses and hence, there will be an error.
5. What is the output of the following code?
for j= 4:2 j=j+1 end
a) an empty vector, j
Explanation: The loop never runs since if we want to decrease the counter value, we will have to introduce the decrement value. The default form, used here, is for increment operation. Hence, the output is an empty vector, j.
6. What is the output of the following code?
for i!=1:10 p=p+1;
a) Syntactical Error
d) Logical Error
Explanation: While mentioning the number of times a loop would run, we have made an error in defining the range. The above statement, really, signifies that the loop won’t run for i= [1,10]. So we did not mention, when the loop will actually run. This results in the error.
7. In nested loops, the break statement exits the ____ loop.
b) last started ongoing
c) not available in MATLAB
Explanation: The break statement exits the loop in which it is only called. It doesn’t care about the remaining number of iterations in the loop but it only exits from the inner loop and the control still remains in the outer loop.
8. In nested loops, the continue statement exits the ____
a) current iteration
Explanation: The continue statement stops the current iteration and restarts a new iteration of the same loop if the condition is not matched. It does not exit from the outer or the inner loop.
9. In nested loops, the break statement, if present within a nested if the structure, will exit the _______
a) Ongoing if structure
b) Entire loop
c) Ongoing loop
d) Entire if structure
Explanation: The break statement will exit the loop where the break statement is present. It won’t take control out of the external loop.
10. In nested loops, the continue statement, if present within an if structure, will exit the ____________
a) If structure
b) current iteration
c) entire loop
Explanation: The continue statement will only end the current iteration and it will start the next iteration. It won’t exit from the entire loop.
11. In nested loops, the continue statement, if present within a nested if structure, will exit the ____________
a) If structure
b) nested if structure
c) entire loop
d) present iteration
Explanation: If the continue statement is present deep-rooted in a nested if structure, it will only stop the current iteration. It won’t exit the current ongoing loop or the external loop.
12. What is the output of the following code?
for j= 4:2:2 j=j+4 end
c) Error an empty vector, j
d) Syntactical error
Explanation: The counter never runs since the initial value is less than the final value but the counter value is positive. This breaks the running of the loop and the value of j is an empty vector.
13. While running a loop in MATLAB, it does not require indentation.
Explanation: The following statement is true for MATLAB. One may use it to make it easier to check a loop but MATLAB won’t give an error if we don’t use indentation.
14. The effective working of the continue keyword in the final iteration is same as the effective working of the break keyword.
Explanation: If the continue statement gets executed in the final iteration of a loop, the control breaks the loop. The break statement will exit control irrespective of the number of iterations left and hence the above statement is true.
15. The number of iterations run for any loop by MATLAB is always _____________
a) a positive integer
b) a negative integer
c) a number
d) a decimal number
Explanation: It is not necessary that a loop will do at least one iteration. If the condition of a while loop is not satisfied before the first iteration, it won’t iterate even once and the number of iterations is 0.
Sanfoundry Global Education & Learning Series – MATLAB.
To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers.