MATLAB Questions and Answers – Branching – 2

This set of MATLAB Question Bank focuses on “Branching – 2”.

1. The if-else structure does not allow ___________
a) Return statement
b) End statement
c) Endif statement
d) Break statement
View Answer

Answer: d
Explanation: The break statement is not allowed within an if-else structure, it is strictly for a loop structure. We can use the return statement and the end statement but the break statement will give an error.

2. What is the output of the following code?

if((-10 &&00)||(20134 && 900))
fprintf("%s","True.")
else
fprintf("%s","False.")
end

a) True
b) False
c) Error
d) Nan
View Answer

Answer: a
Explanation: The logical && will give 0 if and only if one of the anding value is 0. So -10&&00 returns 0 but 20134&&900 gives 1. The logical || gives 0 if both the values are 0. But here, 0||1 is 1. Since the logical expression yields 1, the condition is satisfied and the output is True.
Output: True
advertisement
advertisement

3. What is the output of the following code?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
if(1)
p=9;
end

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

Answer: b
Explanation: The condition has no kind of operator. But we have given the value 1 as a condition. So, the condition will be taken as true and control will execute the set of statements within the if structure. Now we have placed a ‘;’ so the output will be suppressed but p=9.
advertisement

4. What is the output of the following code?

num=2;
switch num
case[2 3 4]
p=9;
case[1 5 6]
q=9;
end

a) No output
b) p=9
c) q=9
d) Output suppressed but p=9
View Answer

Answer: a
Explanation: When we are giving multiple values for a single case, we have to give those values within {}. Since the code contains [], the switch-case structure won’t work.
advertisement

5. What will be the output of the following code?

switch num
case '2'
p=2;
case '2'
p=4-5;
end

a) p=2
b) Error
c) No output
d) p=-1
View Answer

Answer: c
Explanation: We observe that we have given the same case value for multiple cases. This will not generate an error in MATLAB but the switch-case structure won’t work properly and MATLAB will not give any output.

6. Before starting a new case, we have to end the previous case using ___________
a) Break statement
b) Continue statement
c) Return statement
d) No statement
View Answer

Answer: d
Explanation: MATLAB is not like C. So, we don’t have to give the break statement every time we end a case. We can write the next case and during runtime, the control will leave the switch-case structure after executing the matched case. We can give the break statement during looping and the return statement during if-else structure.

7. We cannot use a ____ statement in the standalone if-else structure.
a) break
b) if-else
c) return
d) switch-case
View Answer

Answer: a
Explanation: If the if-else structure is not within any loop, we cannot use the break statement. The break statement is only allowed for loops. If the if-else structure is part of a loop, we can use the break statement to end the loop before it reaches its limit.

8. The continue statement can be used in a switch-case structure.
a) True
b) False
View Answer

Answer: b
Explanation: The continue statement is only allowed for and while loops. If we give the continue statement within a switch-case structure, MATLAB will give an error.

9. We cannot use a ____ statement if the if-else structure is not part of a loop.
a) continue
b) if-else
c) clc
d) plot()
View Answer

Answer: a
Explanation: If the if-else structure is not within any loop, we cannot use the continue statement. The continue statement is only allowed for ‘for’ and ‘while’ loop.

10. What will be the output of the following code?

if(cos(Inf)==NaN)
p=1;
end

a) p=1
b) Output suppressed but p=1
c) Error
d) No output
View Answer

Answer: d
Explanation: The condition sin(Inf)==NaN is not satisfied because MATLAB knows sin(Inf) has a value between 1 and -1, even though it cannot comprehend it. Hence, it will produce a logical 0 and the control will exit the if structure, it will give no output.

11. What will be the output of the following code?

switch num
case ‘a’
p=394;
case ‘b’
c=0;
end

a) Error
b) No output since we haven’t given num a value yet
c) p=394
d) c=0
View Answer

Answer: a
Explanation: We haven’t declared num as symbolic. So we cannot use it as our switching value. Hence, MATLAB will give an error. It wouldn’t have generated any output since we haven’t assigned a character with num, but first it will show an error.

Sanfoundry Global Education & Learning Series – MATLAB.

To practice MATLAB Question Bank, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). 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!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.