MATLAB Multiple Choice Questions on “More about Loops – 1”.
1. What is the output of the following code?
a) Output is suppressed
b) Output shows 1 3 times
c) Output shows 1 2 times
d) Error
Answer: a
Clarification: 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?
a) Infinite loop
b) -1
c) Output is suppressed
d) +1
Answer: b
Clarification: 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) Syntactical Error
b) 3
c) 2
d) Keyword Error
Answer: d
Clarification: 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?
a) Syntactical error
b) Declaration error
c) 3
d) 4
Answer: a
Clarification: 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?
a) an empty vector, j
b) 3
c) 2
d) error
Answer: a
Clarification: 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?
a) Syntactical Error
b) Inf
c) NaN
d) Logical Error
Answer: d
Clarification: 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.
a) external
b) last started ongoing
c) not available in MATLAB
d) for
Answer: b
Clarification: 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
b) loop
c) inner
d) outer
Answer: a
Clarification: 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
Answer: b
Clarification: 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
d) Error
Answer: b
Clarification: 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
Answer: b
Clarification: 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?
a) Error
b) j=6
c) Error an empty vector, j
d) Syntactical error
Answer: c
Clarification: 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.
a) True
b) False
Answer: a
Clarification: 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.
a) True
b) False
Answer: a
Clarification: 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
Answer: c
Clarification: 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.