MATLAB Questions and Answers Part-7

1. Graphs are not suppressed by the ‘;’.
a) True
b) False

Answer: b
Explanation: The commands to generate any kind of graph will always result in an opening of a new window when they are used as required. The ‘;‘ won’t suppress it.

2. What will be the output of the following code?
close all;
for x = 1.5 : .5 : 2;
y=3;
x=y+3
clc
end
a) No value will be stored
b) The result will be printed
c) The result will be printed twice but no value will remain in the Workspace
d) The loop will not run

Answer: c
Explanation: The loop will run twice and the final value of y is 5 while that of x is 2. Each time the loop runs, no result shall be printed since the ‘ ; ‘ at the end of the last line suppresses the result and it gets stored in MATLAB.

3. What will be the output of the following code?
for i = 1 :4: 5
y=i+1
clear i
end
a) y=5; i=2
b) y=5, printed twice
c) Loop will run once, y=2
d) Infinite loop

Answer: b
Explanation: The index of a loop is a separate variable. It will get added to y in the first iteration and y will have value 2. Now even though we have the index i=1 in the workspace, it will get cleared from it but the loop will continue with i=1. So the final value of y will be y=5+1=6. The loop won’t be an infinite loop.

4. Clear will removes all graphs.
a) True
b) False

Answer: b
Explanation: Clear all removes variables from the workspace. It does not remove graphs.

5. What will be the output of the following code?
for i=1 : 3
i=i-1
end
a) i will be printed thrice as 0,1,2 successively
b) Infinite loop
c) No output
d) i=2

Answer: a
Explanation: The index of a loop is a separate variable. The value of I at the first iteration is 1 so at the end of the first iteration, it will print 0. This does not mean that the index variable will also be 0. Thus So i will be printed thrice as 0,1,2 successively.

6. How can we close all graphs in MATLAB?
a) Using the close command
b) Using the clear all command
c) Using the close all command
d) Using the clear command

Answer: b
Explanation: The in-built command to close the windows containing graphs is ‘close all’. This command should be placed at the beginning of a function which creates graphs so that future commands do not affect the graph generated every time the function is called.

7. clc in a function will clear the function
a) True
b) False

Answer: b
Explanation: Suppose we have the clc command at the end of an M-file. The purpose of clc command is to clear the command window. So when we call the function, it will clear the command window but the function will not be cleared.

8. What will be the output of the following code?
for i = 1 : 1
p=i-1
i=i+1;
clc
end
a) i=2
b) p will be printed
c) Error
d) Cannot be determined

Answer: a
Explanation: The value of i won’t be printed since we have ended it with a semi-colon. The value of p will get printed and then the clc command will remove it from the command window. Instead, the value of I will be stored in the workspace as 2.

9. ___________ will give a hint that the file is closed.
a) Fopen
b) Fclose
c) Gclose
d) clc

Answer: b
Explanation: The Fclose command will close the file and returns an integer value to the monitor screen to signify the operation of the command. If it returns 1, the file is closed.

10. To delete one variable but not the entire workspace, we need to mention the variable name after the clear command.
a) True
b) False

Answer: a
Explanation: The clear command removes every variable from the workspace. If we only mention the variable name, it will delete only that variable from the workspace and not the entire workspace will be cleared.