Loops

MATLAB

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

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

3. What is the output of the following code?

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

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

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

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

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

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

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

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

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

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

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *