0% found this document useful (0 votes)
50 views10 pages

Assignment3 L9-L12

Uploaded by

Smita Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views10 pages

Assignment3 L9-L12

Uploaded by

Smita Agarwal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

MOOC Course - Foundations of R Software

July 2024

Assignment 3

1. Which one of the following is the correct outcome of the command

c(6,4,2,1)*c(1,2,3,4)**c(4,2,2,1)+c(4,4,10,10)/c(2,2)?

a. 6.00 6.00 10.67 8.33


b. 8 18 23 9
c. 9 23 18 8
d. Error

Solution: Basic arithmetic of vectors shows that c(6,4,2,1)*c(1,2,3,4)**c(4,2,2,1) gives 6 16


18 4 and then operating c(4,4,10,10)/c(2,2) outputs 2 2 5 5 then adding the two provides
the result.

2. Which one of the following is the correct outcome of the command

c(2,6,3,2)/c(2,3,3,2)+ c(4,2,4,2)^c(2,3)-c(2,4,8,6)/c(2,2)+ c(-3,-


8,-2,-4)*c(3,1) ?

a. 7 0 7 2
b. 5.000000 7.000000 53.000000 7.333333
c. 5 -7 -5 -7
d. None of these

Solution: Again, in this problem, we can use basic arithmetic and BODMAS rule to arrive
at option a.

1
3. Which one of the following commands will give a result without a warning?

a. c(111,123,215,17)^c(16,27,8) + c(23,19,17,35)**c(12,3,14,15)-
c(240,30,76,98)*c(8,3)
b. c(111,123,215,17)^c(16,28) + c(23,19,17,35)**c(12,3,14,15)-
c(240,30,76,98)*c(8,3)
c. c(11,13,15,17)^c(5,7) + c(12,13,14,15)**c(12,3,15)- c(240,30,
76,98)*c(8,3)
d. c(11,13,15,17)**c(8,9,4,7) + c(12,13,14,15)*c(12,3,14,15)-
c(240,30,76,98)/c(8,3,7)

Solution: Second options have all the vector operations that are compatible with each
other. In first option, c(111,123,215,17)^c(16,27,8) is not of compatible length.
Similarly, c(23,19,17,35)**c(12,3,15) and c(240,30, 76,98)/c(8,3,7) are not of
compatible length in option c and d respectively.

4. Which one of the following is the correct outcome of the command

max(c(12,6,7,8)*c(3,6,7,2))+ sqrt(c(12,14,12,14)*c(12,12)+
c(11,21,14,4)**c(2,2,3,1)) ?

a. 65.27882 73.67793 102.74012 62.11448


b. -3.605551 -25.396850 -51.468437 -13.711309
c. 51.468437 13.711309 3.605551 25.396850
d. Error:...

Solution: In this problem again, the R uses basic arithmetic and BODMAS to arrive at
option a.

2
5. Which one of the following is the correct outcome of the command

c(12,3,2,1)**c(1,2,-1,-2)*c(-2,-3,2,3)*c(-3,2,-2,-3)?

a. 72 52 2 9
b. 72 -54 -2 -9
c. -1 1 33 73
d. Error

Solution: In this problem, c(12,3,2,1)**c(1,2,-1,-2) is operated first according to the


BODMAS rule and we can use basic arithmetic to arrive at option b.

6. Which one of the following is the correct outcome of the command

c(115,206,157,138)%/%4 + c(124,244,222,334)%/%c(4,5,4,6) +
c(343,36,67, 88)%%c(21, 33) ?

a. 102 111 98 66
b. 66 111 98 102
c. 66 102 98 111
d. 111 98 66 102

Solution: In this problem, c(115,206,157,138)%/%4, c(124,244,222,334)%/%c(4,5,4,6),


and c(343,36,67, 88)%%c(21, 33) is operated first and we can add these results to get the
option c.

3
7. Which one of the following is the correct outcome of the command

min(c(88,98,144,176)**c(-1/2,1/2))/min(c(81,144,22,38)^c(1/2,-3
)) -prod(c(3,2,3,2)^c(2,3))/max(c(81,24,44,99,26)*min(c(4,3,8
,6)))- max(c(12,24,24,12)^c(2,3))-1/2 ?
a. 23490
b. -234990
c. 234990
d. None of these

Solution: In this problem, the R uses simple arithmetic and BODMAS rule on vectors to
arrive at the option c.

8. Which one of the following is the correct outcome of the command?


round(sum(c(13,22,31,12)^c(2,3)) - sum(c(31,12,22,8)^c(2,3)) -
prod(c(3,2,3,2)^c(1,2,2,3)) + ceiling(c(15,16,17,18)^c(3,2)))

a. 12332 9281 9213 13870


b. 12332 9213 13870 9281
c. 92870 9281 177376 177444
d. None of these

Solution: In this problem, prod(c(3,2,3,2)^c(2,3)) * sum(c(3,2,3,2)^c(2,3)) +


prod(c(3,2,3,2)^c(1,2,2,3)) is operated first which gives 177120. Then adding
ceiling(c(15,16,17,18)^c(3,2)) gives the result. Applying round function doesn’t change
anything, since the result is already integer vector.

4
9. Which one of the following is the correct outcome of the command
floor(c(5,1,7,8)^c(3,2))-ceiling(c(1,6,1,8)^c(2,2)) +
ceiling(c(2,3,4,5)^c(-1,-2)) + round(c(5,6,7,8)^c(3,2)) +
floor(c(5,6,7,8)^c(3,2)) ?

a. 375 129 1029 38


b. 375 38 1029 129
c. 129 38 375 1029
d. None of these

Solution: In this problem, a similar calculation to Q8 gives the required result.

10. Which one of the following is the correct outcome of the command

ceiling(c(4,8,4,5)^c(-3,-9) - sqrt(c(3,6,8,6)**c(3,2))*sqrt(c(2
,3,4,5)^c(3,2)) - round(c(4,7,8,9)*round(c(7,9)))) ?
a. -42 -80 -237 -110
b. -80 -110 -237 -36
c. 42 80 237 110
d. None of these

Solution: In this problem, a similar calculation to Q9 gives the required result.

11. Which one of the following is the correct outcome of the command

ceiling(prod(c(1,5,2,6))+ sum(c(21,22,23,24,25))) -
floor(prod(c(1,5,2,6))- round(c(21,22,23,24,25))) ?

a. 36 37 38 39 40
b. 236 237 238 239 240
c. 136 137 138 139 140
d. None of these

Solution: In this problem, a similar calculation to Q9 gives the required result.

5
12. Which one of the following is the value of X2 when the following commands are
executed over the R console?

X1 = c(12,16,8,14)

X2 = sqrt(X1^2)-X1*2/X1^2+X1**(1/2)+abs(X1)

a. 27.29743 35.87500 18.57843 31.59880


b. 27.29743 31.59880 35.87500 18.57843
c. 18.57843 31.59880 27.29743 35.87500
d. None of these

Solution: In this problem, X1**(1/2) is calculated first, then 2/X1^2. After which, the rest of
calculations are done according to the BODMAS rule.

13. Which one of the following is the correct outcome of the command

c(2,2,4,4)^c(-3,-2,2,3)/c(4,3,2,1) +
c(2,4,1,6)%/%c(3,4)*c(2,5,3,6)+
max(c(20,10,14,15)^c(1,2))/min(c(6,2,7,15)^c(2,2))?

a. 5.27 10.68 23.00 15.00


b. 56.28 -61.33 126.25 64.25
c. 56.28 61.33 64.25 126.25
d. None of these

Solution: In this problem, a similar calculation to Q2 gives the required result.

6
14. Which one of the following is the correct command to obtain the following matrix?

11 14 17
𝑥 = (12 15 18)
13 16 19

a. x=matrix(11:19,3,3,byrow=T)
b. x=mat(11:19,3,3, byrow=T)
c. x=matrix(11:19,3,3,byrow=F)
d. x=mat(11:19,3,3,byrow=F)

Solution: Valid command to form a matrix is matrix(). The data is entered columnwise, so
byrow = F.

15. Which one of the following is the correct command to obtain the following matrix? 𝑧 =
15 8
(11 19)
17 12

a. z = matrix(nrow=2, ncol=3, data=c(15,11,17,8,19,12), byrow=T)


b. z = matrix(nrow=3, ncol=2, data=c(15,8,11,19,17,12) ,
byrow=T)
c. z = matrix(nrow=2, ncol=3, data=(15,8,11,19,17,12) , byrow=T)
d. z = matrix(nrow=3, ncol=2, data=(15,8,11,19,17,12) , byrow=T)

Solution: The matrix has three rows and 2 columns, and the data vector in the command
must be contained in c().

7
16. Which one of the following is the correct command to obtain the third column and
second row of a matrix x defined in R?

a. x(3, ) and x( ,2) respectively.


b. x(3, ) and x(2, ) respectively.
c. x[ ,3] and x[2, ] respectively.
d. x[ ,2] and x[3, ] respectively.

Solution: x[ ,ncol] and x[nrow, ] are the standard commands for ncol-th column and nrow-
th row of the matrix x.

17. Which one of the following is the correct outcome of the command X[2,1] for the
matrix constituted by the command X=matrix(45:54,3,3,byrow=F)?

a. 45
b. 46
c. 47
d. 48

Solution: X[2,1] represents the element in the second row and first column, which is 46.

18. Which one of the following is the correct outcome of the commands dim(x) and
dim(y) for the matrices obtained by x=matrix(1:40,4,10,byrow=T) and
y=matrix(41:80,10,4,byrow=F) ?

a. 10 4 and 4 10 respectively.
b. 4 10 and 10 4 respectively.
c. 4 10 and 4 10 respectively.
d. 10 4 and 10 4 respectively.

Solution: The dimensions of the matrix is already given in the commands to be executed,
which are 2 * 25 and 25 * 2 respectively.

8
19. Which one of the following is the correct command to obtain the following matrix? 𝑧 =
5 1 7
( )
8 9 2

a. z <- matrix(nrow=2, ncol=3, data=c(5,1,7,8,9,2), byrow=T)


b. z <- matrix(nrow=3, ncol=2, data=c(5,8,1,9,7,2) , byrow=T)
c. z <- matrix(nrow=2, ncol=3, data=(5,8,1,9,7,2) , byrow=T)
d. z <- matrix(nrow=3, ncol=2, data=(5,8,1,9,7,2) , byrow=T)

Solution: The command in R to create a matrix is x=matrix(data, nrow, ncol,


byrow=)

20. Which one of the following is the correct outcome of the command

x <- diag(c(3,7), nrow=2, ncol=2) ?

3 7
a. 𝑥 = ( )
0 7

0 3
b. 𝑥 = ( )
7 0

3 7
c. 𝑥=( )
3 7

3 0
d. 𝑥=( )
0 7

Solution: The command diag() creates a diagonal matrix in R with diagonal entries given
by data.

9
MOOC Course - Foundations of R Software

Answers of Assignment 3
1. b

2. a

3. b

4. a

5. b

6. c

7. c

8. b

9. b

10. a

11. c

12. a

13. c

14. c

15. b

16. c

17. b

18. b

19. a

20. d

10

You might also like