MATLAB Questions and Answers – Branching – 1

«
»

This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer

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.
advertisement

2. 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
View Answer

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.
advertisement
advertisement

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

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.
advertisement

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
View Answer

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.

5. What is the output of the following code?

advertisement
syms x;
if(x>50)
fprintf('Error')
end

a) Error
b) Prints Error
c) Does not print error
d) Cannot be determined
View Answer

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’.
advertisement

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

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.

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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: a
Explanation: 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) []
View Answer

Answer: a
Explanation: 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
View Answer

Answer: a
Explanation: The steps of execution in the if structure follows a hierarchy of checking conditions. If the condition is not satisfied, the control breaks from the structure and goes to execute the next state of statements.

13. The switch-case structure is a _________
a) Conditional structure
b) Logical structure
c) Multidimensional structure
d) Hierarchical structure
View Answer

Answer: b
Explanation: Any case in the switch structure gets executed if the switching variable and the case value satisfies the logical condition of equality. Hence the switch-case is a logical structure since based on the equality of the two values, the case will get executed.

14. What is the output of the following code?

if()
p=9;
end

a) Error
b) p=9
c) Output is suppressed but p=9
d) 0
View Answer

Answer: a
Explanation: We cannot keep the condition as empty. We need to give a condition otherwise control will never proceed to allow us to design the if structure. It will show an error as soon as we hit enter after if(). The output could’ve been 9 and it would’ve been suppressed due to the ‘’, but this code will show an error.

15. What is the output of the following code?

if(x=99)
var=92;
end

a) Error
b) var=92
c) Output is suppressed but var=92
d) Nan
View Answer

Answer: a
Explanation: We cannot use an assignment operator in our condition statement while using an if-else structure. We have to use a relational or logical operator only.

Sanfoundry Global Education & Learning Series – MATLAB.

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