This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Logical Expressions – 1”.
1. If a logical expression is true, it will always return a logical 1 and if it’s false, it’ll always return a 0.
a) True
b) False
View Answer
Explanation: Any logical operation is pre-defined in MATLAB to give a logical 1 if the result derived from the operation is true. If the result is false, the result returned will be a logical 0.
2. The functioning of all() and any() is same.
a) True
b) False
View Answer
Explanation: The all() function returns a 1 iff all the elements in the input vector is non-zero. The any() function returns a 1 iff at least 1 element in the input vector is non-zero. Hence, they are different in functioning.
3. 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
View Answer
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.
4. What will be the output of the following code?
all([1 0])
a) 1
b) 0
c) Error
d) Nan
View Answer
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.
5. Choose the correct hierarchy of operations.
a) |,&,+,-
b) +,-,&,|
c) :,+,-,&
d) |,:,&,.^
View Answer
Explanation: Amongst the given operators, the hierarchy is [.^], [+], [-], [:], [&], [|]. Hence, the only possible option is +,-,&,|. The rest are erroneous according to the pre-defined hierarchy in MATLAB.
6. What is the output of the following code?
2++++++++9
a) 11
b) 74
c) 25
d) Error
View Answer
Explanation: Since + is a unary operator, it won’t give an error in MATLAB. Hence, the output of the above code will be 11.
Output: 11
7. What is the output of the following code?
sin(Inf) & sin(nan)
a) Error
b) 1
c) 0
d) Nan
View Answer
Explanation: Nan cannot be used while dealing with logical operators. Hence, the above code will give an error.
8. What will be the output of the following code?
all([Nan])
a) 1
b) 0
c) Error
d) Nan
View Answer
Explanation: Nan is not defined in MATLAB. It should have been ‘NaN’ or ‘nan’. If it was either of them, the result would’ve been 1 since NaN is not equal to 0.
9. What will be the output of the following code?
~xor(3476,1234)
a) 1
b) 0
c) Error
d) NaN
View Answer
Explanation: The output of xor(3476,1234) will be 0 since xor() returns a logical 1 iff one of the argument is 0. Since we have added the not operator before, the output will be 1.
Output: 1
10. What will be the output of the following code?
~or(~xor(1,1),~or(0,1))
a) 1
b) 0
c) NaN
d) Error
View Answer
Explanation: The output of ~or(0,1) is 0. The output of ~xor(1,1) is 1. Hence, the output of ~or(1,0) is 0.
Output: 0
11. What will be the output of the following code?
any(sin(0), NaN)
a) 1
b) Error
c) 0
d) No such function
View Answer
Explanation: The any() function returns 1 if any element, within the input vector, is non-zero. Since NaN is non-zero, the output will b 1.
Output: 1
12. What will be the output of the following code?
any(xor(1,1), ~or(xor(1,0),1), sin(pi), NaN)
a) 0
b) 1
c) Error due to too many arguments
d) Error due to NaN
View Answer
Explanation: The all() function takes a single vector as input. If we had placed all the elements within [], there wouldn’t have been any error and the output would’ve been 1. NaN won’t give any error infact the occurrence of NaN will give the output 1 or the output would’ve been 0.
13. What will be the output of the following code?
all([or(1,1), ~xor(xor(1,0),1), cos(pi)])
a) 0
b) 1
c) Error
d) No such function all
View Answer
Explanation: The all() function returns 1 iff all the elements within the input vector is non-zero. Here, xor(1,0) is 1 and ~xor(1,1) is 1. or(1,1) is 1 and cos(pi) is -1. Thus, all the elements are non-zero.
Output: 1
14. What will be the output of the following code?
all(sin(pi), 1)
a) 0
b) 1
c) Error
d) NaN
View Answer
Explanation: sin(pi) is not 0 since pi is just a floating point variable in MATLAB. The output of sin(sym(pi)) would’ve been 0. Thus the output of the above code is 1 since the function all() returns 1 iff all the elements of the input vector is non-zero.
Output: 1
15. The correct hierarchy of operations is ________
a) <.<=,|,&
b) >,>=,&,|
c) <,>=,|,&
d) <,|,>,&
View Answer
Explanation: Amongst the given operators, the correct hierarchy is <, <=, >, >=, &, |. Thus the most plausible >,>=,&,| according to the pre-defined hierarchy of operations in MATLAB.
Sanfoundry Global Education & Learning Series – MATLAB.
To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers.