MATLAB Questions and Answers Part-27

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

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

3. Menu-driven architecture should be done by ________
a) if-else structure
b) switch-case structure
c) stack
d) loop structure

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.

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

5. Giving conditions using characters, we give them using ____
a) ‘’
b) ””
c) No special character
d) []

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

6. The if structure is a _________
a) Conditional structure
b) Logical structure
c) Nested structure
d) Biased structure

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.

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

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.

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

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.

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

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.

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

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.