MATLAB Questions and Answers – More about Loops – 2

«
»

This set of MATLAB Questions and Answers for Campus interviews focuses on “More about Loops – 2”.

1. The for loop performs at least ___ iteration/s.
a) 1
b) not necessarily an iteration
c) 2
d) Error
View Answer

Answer: b
Explanation: Suppose the condition given to check by the for loop has a logical error- the for loop won’t iterate even once. Hence, it is not necessary that the for loop will run even once.
advertisement

2. Multiple graphs can be plotted, in the same window, if we use the ___ command in a loop.
a) hold on
b) held on
c) hold off
d) not possible
View Answer

Answer: a
Explanation: The hold on command can be used to plot multiple graphs in the same window by using a loop. Hold off is used to exit the functioning of the hold on command. Hence, the correct option is hold on.

3. The number of iterations in the following loop is

advertisement
advertisement
p=3;for i=(3:4)
p=p+2;
end

a) 0
b) 1
c) 3
d) 4
View Answer

Answer: d
Explanation: If the counter value is <= the final value, the loop will perform an iteration. Hence, the above loop iterates twice to yield p=6. There is no error.
advertisement

4. What is the output of the following code?

q=0;for i=(1:1)
q=q+5;
end

a) Output is suppressed
b) Syntactical error
c) q=5
d) Declaration error
View Answer

Answer: a
Explanation: The output is suppressed due to the semi-colon after q=q+5. The output of the following code would’ve only been q=5 if the output was not suppressed. There is no error in the above code.
advertisement

5. What is the output of the following code?

advertisement
a=0;for i=[1:1]
q=a-55;
end

a) Syntactical Error
b) Declaration error
c) Output is suppressed
d) q=-55
View Answer

Answer: c
Explanation: The expression for counter can be given within [] in the for statement. The output is suppressed due to the semi-colon after q=a-55. The output of the following code would’ve only been q=-55 if the output was not suppressed. There is no error in the above code.

6. What is the nature of the error in the following statement?

for i=1:

a) Incomplete
b) Unexpected operator
c) Invalid keyword
d) No error
View Answer

Answer: a
Explanation: MATLAB would recognize that the above statement for starting the for loop is incomplete. Hence, it will give the option incomplete as an error.

7. What is the nature of the error in the following statement?

for i=:1

a) Incomplete syntax
b) Unexpected MATLAB operator
c) No such keyword
d) No error
View Answer

Answer: b
Explanation: The syntax of the for loop is not recognized by MATLAB. Hence, it will give the unexpected MATLAB operator as an output.

8. What is the output of the following code?

i=0; while[i<5] 
i=i+1;
end

a) Syntactical Error
b) Declaration Error
c) Output is suppressed
d) i=5
View Answer

Answer: c
Explanation: There is no error due to []. The output is suppressed due to the ‘ ; ‘. The value of I stored in the workplace is i=5.

9. The condition of a while loop cannot be given in ________
a) ()
b) []
c) {}
d) <>
View Answer

Answer: c
Explanation: The variables of the condition gets converted to a cell format if the condition is given in {}. Thereafter, the variables cannot be converted into a logical form. It can be given within () and [].

10. What is the output of the following code?

while{i<5}
i=i+1234;
end

a) i=1234
b) Output is suppressed
c) Error in converting the cell to logical form
d) Error in i
View Answer

Answer: c
Explanation: MATLAB returns the first error it finds. Now, i is not defined before using it as a variable for a condition in the while loop. But the first error is the fact that the condition is given in {} so it is in a cell format and the conversion to logical form is not possible. Hence, MATLAB will return an error.

11. What is the output of the following code?

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

a) Error
b) i=-Inf
c) i=NaN
d) Infinite loop
View Answer

Answer: d
Explanation: The following code will start an infinite loop. The control can be broken form the loop by pressing ctrl+c.

Sanfoundry Global Education & Learning Series – MATLAB.

To practice all areas of MATLAB for Campus Interviews, 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