MATLAB Questions and Answers – Arithmetic – 1

This set of MATLAB Multiple Choice Questions & Answers (MCQs) focuses on “Arithmetic – 1”.

1. What would be the output of the following code (in editor window)?

A = [0 1; 1 0]	;	B=2	;	C = A + B

a)

   1	2
   4	5
advertisement
advertisement

b)

    2	3
    3	2

c)

    3	2
    3	2
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

d)

    3	2
    2	3
View Answer
Answer: b
Explanation: C = A + B adds arrays A and B and returns the result in C.
C = plus(A,B) is an alternate way to execute A + B, but is rarely used. It enables operator overloading for classes.
 
 

2. What would be the output of the following code (in editor window)?

advertisement
A = [1 0 2]	;	b = [3 0 7]	;	c=a.*b;

a) [2 0 21]
b) [3 0 14]
c) [14 0 3]
d) [7 0 3]
View Answer

Answer: b
Explanation: C = a.*b multiplies arrays a and b element by element and returns the result in C.
advertisement

3. What would be the output of the following code (in editor window)?

a=1:5	;	c=a.^2

a) [1 25]
b) [1 2 3 4 5]
c) [25 16 9 4 1]
d) [1 4 9 16 25]
View Answer

Answer: d
Explanation: c=a.^2 raises each element of a to the corresponding power i.e. 2. It will square each element.
[12 22 32 42 52] = [1 4 9 16 25].

4. What would be the output of the following code (in editor window)?

A = [1	1	0	0]
B = [1	;2	;3	;4]
C=A*B

a) 0
b) [1 0 0 0]
c) 3
d) [1 2 0 0]
View Answer

Answer: c
Explanation: The result is a 1-by-1 scalar, also called the dot product or inner product of the vectors A and B. Alternatively, you can calculate the dot product A • B with the syntax dot(A,B).
Multiply B times A.

5. What would be the output of the following code (in editor window)?

A = [1	2;	3	4]
C = A^2

a) [7 10; 15 22]
b) [1 4; 9 16]
c) [16 9; 4 1]
d) [22 15; 10 7]
View Answer

Answer: a
Explanation: C = A2 computes A to the 2 power and returns the result in C. The syntax A2 is equals to A*A.
A2 = [1 2; 3 4] *[1 2; 3 4] [7 10; 15 22].

6. What would be the output of the following code (in editor window)?

A=1:5;
B=cumprod(A)

a) b=[1 2 6 24 120]
b) b=[1 2 3 4 5]
c) b=[5 4 3 2 1]
d) b=[120 24 6 2 1]
View Answer

Answer: a
Explanation: B = cumprod(A) returns the cumulative product of A starting at the beginning of the first array dimension in A whose size does not equal 1. B(2) is the product of A(1) and A(2), while B(5) is the product of elements A(1) through A(5).

7. Create an array of logical values.

A = [true false true; true true false]
A = 1     0     1
    1     1     0
B = cumprod(A,2)

Find the cumulative product of the rows of A.
a)

      B = 1     0     0
          0     1     0

b)

      B = 1     0     0
          1     1     0

c)

      B = 1     0     0
          1     1     1

d)

      B = 1     1     0
          1     1     0
View Answer
Answer: b
Explanation:

      B = 1     0     0
          1     1     0

The output is double.
class(B)
ans = double.

 
 

8. Find the cumulative sum of the columns of A.

  A =1     4     7
     2     5     8
     3     6     9
B = cumsum(A)

a)

B = 1     4     7
    3     8    15
    6    15    24

b)

B = 1     4     7
    4     9    15
    4    15    24

c)

B = 1     4     7
    3     9    15
    6    15    29

d)

B = 1     4     7
    3     9    15
    6    15    24
View Answer
Answer: d
Explanation: B = cumsum(A) returns the cumulative sum of A starting at the beginning of the first array dimension in A whose size does not equal 1. If A is a matrix, then cumsum(A) returns a matrix containing the cumulative sums for each column of A. B(5) is the sum of A(4) and A(5), while B(9) is the sum of A(7), A(8), and A(9).
 
 

9. Create a 4-by-2-by-3 array of ones and compute the sum along the third dimension.

A = ones(4,2,3);
S = sum(A,3)

a)

S = 3     3
    3     3
    3     3
    3     3

b)

S = 3     4
    3     4
    3     4
    3     4

c)

S = 2     3
    2     3
    2     3
    2     3

d)

S = 7     3
    5     3
    6     3
    3     3
View Answer
Answer: a
Explanation: S = sum(A) returns the sum of the elements of A along the first array dimension whose size does not equal 1. If A is a multidimensional array, then sum(A) operates along the first array dimension whose size does not equal 1, treating the elements as vectors. This dimension becomes 1 while the sizes of all other dimensions remain the same.
 
 

Sanfoundry Global Education & Learning Series – MATLAB.

To practice all areas of MATLAB, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.