MATLAB Questions and Answers Part-28

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

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.

2. What is the output of the following code?
if(1)
p=9;
end
a) p=9
b) Output is suppressed but p=9
c) Error
d) Nan

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

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

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.

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

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.

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

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.

6. 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()

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.

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

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

8. What does the function all() do?
a) Returns 1 if all elements within it are 0
b) Returns 0 if all elements within it are 0
c) Returns 0 if all elements within it are non-zero
d) Returns 1 if all elements within it are non-zero

Answer: d
Explanation: The all() function is inbuilt in MATLAB. If we give a vector input to our function, it will return a 1 if all the elements of the vector are non-zero.

9. What will be the output of the following code?
all([1 0])
a) 1
b) 0
c) Error
d) Nan

Answer: b
Explanation: The all() function returns a logical 1 iff all the elements within the input vector are non-zero. Since, here, one element is 0- the logical output is 0.

10. What is the output of the following code?
sin(Inf) & sin(nan)
a) Error
b) 1
c) 0
d) Nan

Answer: a
Explanation: Nan cannot be used while dealing with logical operators. Hence, the above code will give an error.