MATLAB Questions and Answers – Sums and Products – 2

This set of MATLAB Quiz focuses on “Sums and Products – 2”.

1. What is the output of the following code?

meansqr(NaN)

a) 0
b) NaN
c) Error
d) 1
View Answer

Answer: a
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.
Output: 0
advertisement
advertisement

2. What is the output of the following code?

meanabs(1,2,-1)

a) 1.33
b) 1
c) 0
d) .666
View Answer

Answer: a
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.
Output: 1.33
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. What is the output of the following code?

advertisement
A=[1:3:7;2:3:8;3:3:9]; prod(A,1)

a) Error
b) 6 20 504
c)

    6
    20
    504
advertisement

d) 28 80 162
View Answer

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

prod(1:NaN)

a) 0
b) 1
c) NaN
d) Error
View Answer

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

5. What is the output of the following code?

cumsum([1,2,3,4,NaN])

a) 1 3 6 10 NaN
b) NaN
c)

    1
    3
    6
   10
  NaN

d) Error
View Answer

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

cumsum[NaN]

a) NaN
b) 0
c) Error due to NaN
d) Syntactical Error
View Answer

Answer: d
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.
a) True
b) False
View Answer

Answer: b
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 __________
a) sqrt(meansqr())
b) sqrt[meansqr[]]
c) sqrt(meansqr[])
d) sqrt[meansqr()]
View Answer

Answer: a
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=[1:3:7;2:3:8;3:3:9]; prod(A,2)

a) 28 80 162
b)

    20
    80
    162

c) Error
d) NaN
View Answer

Answer: a
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?
a) sumcum[]
b) sumcum()
c) cumsum[]
d) cumsum()
View Answer

Answer: d
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?

cumsum(1,2,-1)

a) 1 3 2
b) 2
c) Error due to input
d) Error due to syntax
View Answer

Answer: c
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.
a) True
b) False
View Answer

Answer: b
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 ____________
a) Same
b) Different
c) Absolute numbers
d) Signed numbers
View Answer

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

a) Error
b) -1/(a-1) -1
c) -1/(a-1)
d) Inf
View Answer

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

a) 1/1-a
b) Inf
c) Error due to Inf
d) Error due to an unmentioned value of a
View Answer

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

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.