MATLAB Multiple Choice Questions on “Branching – 1”.
1. 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
Clarification: 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.
2. What is the output of the following code?
a) p=0
b) Output will be suppressed
c) Error
d) Cannot be determined
Answer: c
Clarification: 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.
3. The end statement is given multiple times in _________
a) Multiple elseif
b) Nested if
c) elseif
d) if
Answer: b
Clarification: 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.
4. 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
Clarification: 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.
5. 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
Clarification: 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’.
6. While developing a switch-case structure, the value for case can be _________
a) Single only
b) Multiple
c) Infinite
d) 0
Answer: b
Clarification: 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.
7. What is the output of the following code?
syms x c; if(x~=c) fprintf(‘Error’); end
a) Error
b) Error due to x
c) Error due to c
d) Error due to x and c
Answer: a
Clarification: Since we declared x and c with the syms command, they have been assigned to the variable ans. Hence, matlab would have printed ‘Error’. The output won’t be suppressed.
8. What is the output of the following code?
sym x c; if(x~=c) fprintf('Error'); end
a) Error
b) Error due to x
c) Error due to c
d) Output suppressed
Answer: b
Clarification: Since we have used the sym command, x and c will be declared symbolic but they will be assigned to a separate variable. Hence, MATLAB will show an error since it can find the symbolic variable x. If we had used the syms command, MATLAB would have printed ‘Error’. The output won’t be suppressed.
9. Menu-driven architecture should be done by ________
a) if-else structure
b) switch-case structure
c) stack
d) loop structure
Answer: b
Clarification: A Menu implies that we have sections. A switch-case structure offers sections or cases which will be implemented based on the switching value. Hence, this is perfect for a menu-driven architecture.
10. The program to check the highest of n numbers can be done by ________
a) Sorting
b) If-else structure
c) Switch-case structure
d) Depends on the nature of n
Answer: a
Clarification: The sorting procedure is the easiest way to get the highest of n numbers. This is because the code complexity will increase, drastically, if we use an if-else structure or a switch-case structure.
11. Giving conditions using characters, we give them using ____
a) ‘’
b) ””
c) No special character
d) []
Answer: a
Clarification: The ‘’ is used while dealing with characters. If we haven’t declared our characters before using them, we can compare characters by placing them within ‘’.
12. The if structure is a _________
a) Conditional structure
b) Logical structure
c) Nested structure
d) Biased structure
