MATLAB Questions and Answers Part-6

1. What is the default increment value in a for-loop?
a) 0
b) 1
c) An increment value is necessary
d) 0/1

Answer: b
Explanation: When we are going to start a for loop in MATLAB, it is not necessary to assign a specific increment value. The default increment value will then be taken as 1.

2. What is the output of the following code?
for i=1:4
for j=1:4
a) No output will be shown
b) a=10;
c) a=80
d) Error in the code

Answer: b
Explanation: We have put a semi-colon after a=5. So, for both the “for loops” for i and j, we will keep assigning 5 to ‘a’ repeatedly. At last, we will do a=a+5. This will make ‘a’ as 10.

3. How do we break from an infinite loop without keeping a break statement within the loop?
a) Press Ctrl+C
b) Press Ctrl+Z
c) Loop won’t be terminated
d) Press Ctrl+X

Answer: a
Explanation: If we begin an infinite loop, by choice or as a silly mistake, we can terminate the loop manually if we forget to introduce a break statement as necessary. We can choose to press Ctrl + C and the loop will get terminated at the same instant.

4. What is the size of i after the following code is run in MATLAB?
for i=5:1
a) No output
b) 0*0
c) i=-1
d) Error

Answer: b
Explanation: The above loop does not run because the default increment value in MATLAB is +1. We have to assign a decrement value separately if we want the index value to decrease for a for-loop. If we set a decrement value of -1, the loop will run for 5 times and the final value of i will be -1. No output will be shown due to a semi-colon but the question is what will be the size of i. .

5. A break statement will leave the outer loop.
a) True
b) False

Answer: b
Explanation: In case of nested for-loops or while-loops, the break statement will remove control only form the loop where it was invoked as a command. It won’t come out from the outer loop.

6. How many times will the following loop run?
for i=1:5
if(i<3) break
a) No output due to some error
b) 1 times
c) 0 times
d) 3 times

Answer: c
Explanation: The for-loop is not terminated. Any statement or a group of statement, in a for-loop, will be executed effectively only after the end statement is used to terminate the loop.

7. What is the nature of the following code?
for i=1:8
a) j=0 & i=1
b) j=1 & i=0
c) i=8 & j=36
d) Error

Answer: a
Explanation: We find that the inner while loop goes into an infinite loop. MATLAB is unable to compute the value so it returns j=0 as was initialized. The same goes for i=1. If there was no while loop, option i=8 & j=36 would have been the output. There is no error in logic. Output: No output will be shown since we have placed a semi-colon after j=j+i. But the workspace will store i & j as 1 & 0 respectively.

8. A for-loop can have multiple index values.
a) True
b) False

Answer: a
Explanation: A for-loop has a single index. But it has multiple index-values during successive iterations

9. There can be multiple decision variables for while loop.
a) True
b) False

Answer: a
Explanation: We can provide multiple conditions to end a while loop. The conditions can be different so we can have multiple decision variables for while loop.

10. What will be the output for the following code?
while(k<1 && i<1)
a) i=0,k=1
b) i=0,k=0
c) i=1,k=0
d) i=1,k=1

Answer: a
Explanation: We find that i become 0 at the end of the first iteration while k becomes 1. Since k<1 is a condition for the while loop to terminate, the while loop gets terminated. In case of multiple condition, the while loop will be terminated even if one condition is satisfied.