MATLAB Questions and Answers Part-29

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

Answer: c
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.

2.What will be the output of the following code?
~xor(3476,1234)
a) 1
b) 0
c) Error
d) NaN

Answer: a
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

3. What will be the output of the following code?
~or(~xor(1,1),~or(0,1))
a) 1
b) 0
c) NaN
d) Error

Answer: b
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

4. What will be the output of the following code?
any(sin(0), NaN)
a) 1
b) Error
c) 0
d) No such function

Answer: a
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

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

Answer: c
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.

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

Answer: b
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

7.What will be the output of the following code?
all(sin(pi), 1)
a) 0
b) 1
c) Error
d) NaN

Answer: b
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

8. The correct hierarchy of operations is ________
a) <.<=,|,&
b) >,>=,&,|
c) <,>=,|,&
d) <,|,>,&

Answer: b
Explanation: Amongst the given operators, the correct hierarchy is <, <=, >, >=, &, |. Thus the most plausible >,>=,&,| according to the pre-defined hierarchy of operations in MATLAB.

9. ‘=’ is a?
a) Relational operator
b) Arithmetic operator
c) Operational operator
d) Assignment operator

Answer: d
Explanation: p=2 assigns the value 2 to the variable p. Hence, ‘=’ is an assignment operator. It doesn’t perform arithmetic operations so it isn’t an arithmetic operator. It isn’t a relational operator unlike ‘==’.

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

Answer: c
Explanation: sin(pi) is not equal to 0 since pi is represented as a floating point number in MATLAB. If pi was declared as a symbolic variable, the output would’ve been 0.
Output: 1