MATLAB Questions and Answers Part-30

1. What is the output of the following code?
sin(Inf)~=NaN
a) 1
b) 0
c) Error
d) NaN

Answer: a
Explanation: sin(Inf) is actually a number since the domain of sine is -infinity to infinity and the range is always -1 to 1. Hence, sin(Inf) is still a number- but it cannot be comprehended hence the value will be returned NaN. But in the above comparison, MATLAB will observe that sin(Inf) is still a number within -1 and 1 and hence, the output is 1.

2. What is the output of the following code?
log(0)==Inf
a) 1
b) 0
c) Error
d) Inf

Answer: b
Explanation:log(0) is -Inf. This is why the output of the above code is 0.
Output: 0

3. What is the output of the following code?
and(sin(pi),112312)
a) 1
b) 0
c) 00
d) Error

Answer: a
Explanation: sin(pi) won’t return 0 since pi is an in-built floating-point constant. If we had declared pi as symbolic, the output would’ve been 0.

4. The ~ is ______
a) Quaternary operator
b) Relational Operator
c) Arithmetic Operator
d) Unary Operator

Answer: d
Explanation: ~ is a unary operator since the NOT function operates on only one operand. It isn’t a relational operator since it doesn’t do any comparison and it’s not an arithmetic operator since it doesn’t perform any arithmetic operation.

5. What is the output of the following code?
NaN==Inf
a) 0
b) 1
c) Nan
d) Inf

Answer: a
Explanation: Nan and Inf are two symbolic variables in MATLAB suggesting ‘Not a number’ and ‘Infinity’ respectively. These are not equal to each other and hence we will get a logical 0 when we enter this in MATLAB.

6. What is the output of the following code?
Sin(Inf)==NaN
a) 0
b) 1
c) Nan
d) Inf

Answer: c
Explanation: If we type sin(Inf) in MATLAB, we will get NaN as an input because MATLAB cannot find the value of sin(Inf). But, MATLAB knows that sin(Inf) possesses a value, it cannot comprehend it but still it has a value. So, even though it will show NaN when we write sin(Inf), it will not give a logical 1 for the following code since sin(Inf) is still a number between 1 and -1.

7. What is the output of the following code?
~xor(and(1,0),~xor(and(1,0),0))
a) 0
b) 1
c) NaN
d) Error

Answer: a
Explanation: and(1,0) is 0. ~xor(0,0,) is 1. But xor(1,0) is 1. Hence, ~xor(1,0) is 0.
Output: 0

8. The precedence of Transpose operation is ____________
a) after &
b) after |
c) before .^
d) after ^

Answer: c
Explanation: The transpose operation has the highest precedence amongst all the aforementioned operators. The hierarchy is .’, .^, ^, &, |. Hence option before .^ is correct.

9. All relational operators operate on _______________
a) only real part of complex numbers
b) only imaginary part of complex numbers
c) complex numbers
d) numbers which are not complex

Answer: a
Explanation: Some relational operators do not operate on the imaginary part of the complex numbers. But all relational operators operate on the real part of complex numbers.

10. What is the output of the following code?
any([])
a) 0
b) 1
c) NaN
d) Error

Answer: a
Explanation: Since there are no elements in the input vector, it also means there are no non-zero elements in the vector. Hence, the logical result of the operation is 0.