This set of MATLAB Quiz focuses on “Sums and Products – 2”.
1. What is the output of the following code?
Explanation: The fuction meansqr() returns the mean of the square of all finite value inputs. Since NaN is not a finite valued input, there are no other finite valued elements in the input. Hence the result is 0.
2. What is the output of the following code?
Explanation: The meanabs() function converts every input element to it’s absolute value and then evaluates the mean. Hence the absolute vlue of -1 becomes 1 and the final mean is 4/3 or 1.333.
3. What is the output of the following code?
b) 6 20 504
6 20 504
d) 28 80 162
Explanation: prod(A,1) suggests that all the elements of every column are to be multiplied and the result is going to be displayed as a row vector. Hence option c is not correct. Option d would be the transpose of prod(A,2).
Output: 6 20 504
4. What is the output of the following code?
Explanation: The range, we have defined, from 1 consists of NaN. The product of any number with NaN will give an output NaN. Hence, the answer is option c. There will be no error.
5. What is the output of the following code?
a) 1 3 6 10 NaN
1 3 6 10 NaN
Explanation: The function cumsum() would return the cumulative sum of every input element. Since the first 4 elements can be summed, the output will be shown but since the last element is a NaN, the final element will be a NaN. There won’t be any error due to the NaN element and option c is the transpose of option a but cumsum() returns a row vector.
Output: 1 3 6 10 NaN
6. What is the output of the following code?
c) Error due to NaN
d) Syntactical Error
Explanation: The cumsum() function accepts inputs within parentheses. Since we have only placed the NaN as a vector element, but not within parentheses, the result will be an error. NaN won’t give an error, the result would’ve been NaN if it wass given within parentheses.
7. The product of a set of elements is always greater than it’s the cumulative sum.
Explanation: For the set of elements [1,2,-4,2], the cumulative sum becomes 1,3,-1,1 while the product is -16 which is less than 1. Hence, the given statement is false.
8. The function to find the R.M.S value is __________
Explanation: The correct syntax of the function which performs the mean square of a set of elements is meansqr(). The correct syntax of the function which return the square root of a number is sqrt(). Hence, option sqrt(meansqr()) is only syntactically correct while the rest of the options are syntactically incorrect.
9. What is the output of the following code?
a) 28 80 162
20 80 162
Explanation: prod(A,2) would’ve returned option b since giving an input 2 as a dimension sgugests that all the elements in each column are to be multiplied and the result is a single column vector consisting of the product of every element in a row. Since we have added a transpose at the end, the answer is option a. There is no Error.
Output: 28 80 162
10. Which function does cumulative summation of elements?
Explanation: The correct syntax for the function which does cumulative summation is ‘cumsum()’. Hence option cumsum is wrong. The functions in options sumcum() and sumcum do not exist in MATLAB.
11. What is the output of the following code?
a) 1 3 2
c) Error due to input
d) Error due to syntax
Explanation: We cannot give a negative number as an input to our cumsum() command. This is forbidden in MATLAB and hence the error is due to input.
12. The sum function will return a NaN if any of the element in the input vector is NaN.
Explanation: We can write ‘omitNaN’ within the sum function so that while computing sum, the function ignores every NaN element present within the input vector.
Eg: A=[1,2,NaN];sum(A,’omitNaN’)- This will return 3 as answer.
13. The result of cumsum([1,2,3]) and sum([3,3]) are ____________
c) Absolute numbers
d) Signed numbers
Explanation: The result of cumsum([1 2 3]) is 1,3,6 i.e the result is a row vector having output after adding up each element cumulatively while the result of sum([3 3]) is only 6. Hence, the two answers are different.
14. What is the output of the following code?
syms a,k; symsum(a^k, 1, Inf)
b) -1/(a-1) -1
Explanation: We tried to define a and k as symbolic but we’ve placed a ‘ , ‘ after a. So, k never gets defined and hence the output of the symsum function gives an error since k is not defined. If there was no comma, the result would’ve been 1/(a-1) -1 while, if the code was symsum(a^k,0,Inf), the answer would’ve been -1/(a-1).
15. What is the output of the following code shown?
syms a k; symsum(a^k, 0, Inf)
c) Error due to Inf
d) Error due to an unmentioned value of a
Explanation: MATLAB will return an answer intuitively while considering symbolic summations. Here, MATLAB will return an answer which is plausible to a condition. Hence it will give option a, and will also supply the condition for the answer i.e abs(a)<1.
Output: piecewise(1 <= a, Inf, abs(a) < 1, -1/(a – 1))
Sanfoundry Global Education & Learning Series – MATLAB.
To practice all areas of MATLAB for Quizzes, here is complete set of 1000+ Multiple Choice Questions and Answers.