MATLAB Questions and Answers – Loops

«
»

This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Loops”.

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

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.
advertisement

2. What is the output of the following code?

for i=1:4
for j=1:4
a=5;a=a+5;
end
end

a) No output will be shown
b) a=10;
c) a=80
d) Error in the code
View Answer

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.
advertisement
advertisement

3. What is the output of the following code?

advertisement
for i=1:5
for j=1:6
a(i,j)=input();
end
end

a) No output
b) Error
c) Asks user to input a 5*6 matrix
d) Asks and displays a 5*6 matrix
View Answer

Answer: b
Explanation: The syntax for input is wrong. There should be a pair of single inverted commas within the syntax, even though we don’t have to write anything necessarily within them. But to take any input, we have to use the

syntax: a(i,j)=input(‘’) 	OR	a(i,j)=input(‘Enter value coloumn-wise:’)
advertisement

We can choose to place a semi-colon at the end, then the matrix won’t be displayed after every input.

4. What is the size of i after the following code is run in MATLAB?

advertisement
for i=5:1
i=i-2;
end

a) No output
b) 0*0
c) i=-1
d) Error
View Answer

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
View Answer

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
View Answer

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?

j=0;i=1;
while(j>5)
for i=1:8
j=j+i;
end
end

a) j=0 & i=1
b) j=1 & i=0
c) i=8 & j=36
d) Error
View Answer

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
View Answer

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
View Answer

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?

k=0;i=0;
 while(k<1 && i<1)
k=k+1;
i=i+1;i=i-k;
end

a) i=0,k=1
b) i=0,k=0
c) i=1,k=0
d) i=1,k=1
View Answer

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.

11. 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
View Answer

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.

12. What will the following code do?

j=0;i=5;
while(j<0)
j=j-1; i=i+5;j=i;
end

a) Nothing
b) j=0 & i=5
c) Error
d) Cannot be determined
View Answer

Answer: b
Explanation: We observe that the while loop will enter into an infinite loop. Hence the final value of i and j won’t be determined by MATLAB. MATLAB will retain the values for i and j as they were initialized. Hence j will remain 0 and i will remain 5. There is no error in the code.

Sanfoundry Global Education & Learning Series – MATLAB.

To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers.

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!

advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter