MATLAB Questions and Answers Part-26

1. What is the output of the following code?
t=[-2:1:2];plot(t,exp(t));ax=gca;ax.Yscale=’Log’;
a) Error
b) An exponential graph
c) A logarithmic function
d) A ramp function

Answer: d
Explanation: The scale of the y-axis is changed to a logarithmic nature. But an exponential function is plotted in the y-axis and hence the graph is being approximated to a straight line which follows the interval of the input only.

2. Using the set command for axes can be replaced by setting the ___ to a variable.
a) gca
b) gcf
c) gco
d) not possible

Answer: a
Explanation: The gca is used as an object to represent the axes. The properties of the axes are contained in the gca. The gca can be assigned to a variable and now we don’t have to use the set command to edit the axes of the graph. The gcf is used to represent the graph plotted.

3. What is the output of the following code?
plot([-3:1:3],[-3:1:3]);cla;
a) Error
b) A ramp function
c) A graph of a ramp function with no axes
d) A window having two axes

Answer: d
Explanation: A ramp function was plotted due to the plot command. But the cla command removed all graphics object from the window containing the graph and hence the correct option is a window having two axes

4. How many objects are created for 24 texts written in the graph?
a) 22
b) 24
c) 21
d) 2

Answer: b
Explanation: The text command would be created 24 objects in the memory for 24 texts added in the graph. It will be able to access those texts and edit them.

5. Amongst multiple elseif and nested if, which would take less runtime?
a) multiple elseif
b) nested if
c) elseif
d) elseif & nested if

Answer: a
Explanation: If multiple elseif and nested if has the same number of conditions. Multiple elseif checks at each step if it at all it has to go to the next condition. But nested if checks each condition before leaving the entire control structure. Hence, the number of steps covered in multiple elseif is less than nested if. Thus the former will take less time.

6. What is the output of the following code?
if(x~=c)
p=0;
end
a) p=0
b) Output will be suppressed
c) Error
d) Cannot be determined

Answer: c
Explanation: While using relational operators on character variables, we need to use ‘’. Hence, MATLAB gives an error when we write x~=c. If we had placed c within ‘’, the output would have been displayed as p=0

7. The end statement is given multiple times in _________
a) Multiple elseif
b) Nested if
c) elseif
d) if

Answer: b
Explanation: We need to end each if structure within a nested if structure every time we give one. The end command should be placed after the end of each if structure or MATLAB will give an error.

8. What will happen if we don’t give an otherwise block to our switch structure?
a) Error
b) Infinitely demands a correct variable for case
c) Returns 0
d) Moves on to the next block

Answer: d
Explanation: If we don’t give an otherwise block to our switch structure, it will not give any error. It will simply continue with the execution of statements after the switch structure.

9. What is the output of the following code?
syms x;
if(x>50)
fprintf('Error')
end
a) Error
b) Prints Error
c) Does not print error
d) Cannot be determined

Answer: a
Explanation: We have declared x as a symbolic variable and haven’t assigned any value to it. So it is not declared with 0, it is only a symbolic character. Hence, the if structure cannot check the condition because it cannot convert the symbolic form to logical form. Hence it won’t go inside the if structure- it will return an error but not print ‘Error’.

10. While developing a switch-case structure, the value for case can be _________
a) Single only
b) Multiple
c) Infinite
d) 0

Answer: b
Explanation: The switch-case structure has a versatile syntax in MATLAB. We can give many numbers of case values to match with the input. This can help us in solving complex problems where we need selection of values.