Assignment6 L22-L25

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

MOOC Course - Foundations of R Software

July 2023

Assignment 6

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

for(i in 11:13) {print(1 + 2i - i^2)} ?

a.

[1] -98

[1] -119

[1] -142

b.

[1] -120 + i2

[1] -143 + i2

[1] -168 + i2

c.

[1] -120+2i

[1] -143+2i

[1] -168+2i

d.

[1] 98

[1] 119

[1] 142

1
Solution: Here, i in R is recognized as the unit complex number (square root of -1).
This leads to a mathematical operation on i treated as index i, and character in i
treated as imaginary number i.

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

for(i in 2:5) {print(i + 2*i - 3*i**2)} ?

a.

[1] i + 2*i - 3*i**2

[1] i + 2*i - 3*i**2

[1] i + 2*i - 3*i**2

[1] i + 2*i - 3*i**2

b.

[1] -6

[1] -18

[1] -36

[1] -60

c.

[1] 6

[1] 18

[1] 36

[1] 60

2
d. Error...

Solution: Here, i in R is recognized as the unit complex number (square root of -1).
This leads to a mathematical operation on i treated as index i, and character in i
treated as imaginary number i.

3. Which one of the following is the correct outcome of the following commands
about while loop?

y = 5

while (y < 20) {


print(c("MOOC Courses","are helpful."))
y = y + 2
print(c("MOOC Course","is not helpful."))
y = y + 3
}

a.
[1] "MOOC Course" "is not helpful."
[1] "MOOC Course" "is not helpful."
[1] "MOOC Course" "is not helpful."
[1] "MOOC Courses" "are helpful."
[1] "MOOC Courses" "are helpful."
[1] "MOOC Courses" "are helpful."

b.
[1] "MOOC Courses" "are helpful."
[1] "MOOC Courses" "are helpful."
[1] "MOOC Courses" "are helpful."
[1] "MOOC Course" "is not helpful."
[1] "MOOC Course" "is not helpful."
[1] "MOOC Course" "is not helpful."

c.
[1] "MOOC Courses" "are helpful."
[1] "MOOC Course" "is not helpful."
[1] "MOOC Courses" "are helpful."
[1] "MOOC Course" "is not helpful."
[1] "MOOC Courses" "are helpful."

3
[1] "MOOC Course" "is not helpful."

d.
[1] "MOOC Course" "is not helpful."
[1] "MOOC Courses" "are helpful."
[1] "MOOC Course" "is not helpful."
[1] "MOOC Courses" "are helpful."
[1] "MOOC Course" "is not helpful."
[1] "MOOC Courses" "are helpful."

Solution: Here, the while loop runs until value of y is less than 20. Adding 2 and 3
successively, y=5 becomes more than 20 breaks the loop.

4. Which one of the following is the correct outcome of the following while loop?

x = 22

while(x < 25) {x = x^2-20; if (x == 35) break; print(x); }

a.

[1] 464

b.

[1] Inf

c.

[1] -464

d.

[1] 504

Solution: Here, the while loop runs until value of y is less than 25. Additionally, the
loop also breaks when x=35.

4
5. Which one of the following is the correct specification to compute 𝑦 =
√𝑒𝑥𝑝( 𝑥 2 ) + 𝑥 − 𝑥 5/2 + 40 and what is its value for x = 2?

a. y<-function{x}{sqrt(exp(x^2)+x)-x^5/2+40} , y= 41.86632
b. y<-f(x)[sqrt(exp(x^2)+x)-x^(5/2)+40] , y= 27.37477
c. y<-function(x){sqrt(exp(x)+x^2)-x^5/2+40} , y= 27.37477
d. y<-function(x){sqrt(exp(x^2)+x)-x^(5/2)+40} , y = 41.86632

Solution: While defining a function, the arguments can be encased in small or curly
braces. Square braces are not valid. Also, x^5/2 means (x^5) / 2.

6. Which one of the following is the correct outcome of z(14,11) of the function spe
cified as z=function(x,y){sqrt(x^3-y^3+log(x))+log(-(5*x^-2+6*y^-
2-60))-(x^3-y^2)^(3/4)} ?
a. -4511644550
b. -324.8028
c. -2758.699
d. None of these

Solution: The answer can be had by simply executing the function in command line.

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

abs(seq(-40,-45))?

a. [1] 40 41 42 43 44 45

b. [1] -40 -41 -42 -43 -44 -45

c. [1] 45 44 43 42 41 40

d. [1] -45 -44 -43 -42 -41 -40

Solution: Here, seq() creates a sequence from -40 to -45 then abs() takes it’s mod.

5
8. Which one of the following is the correct output of the command
sqrt(abs(seq(-25,25, by = 10))) ?

a. -5.000000 -3.872983 -2.236068 2.236068 3.872983 5.000000

b. 5.000000 3.872983 2.236068 2.236068 3.872983 5.000000

c. 2.236068 3.872983 5.000000 5.000000 3.872983 2.236068

d. -2.236068 -3.872983 -5.000000 5.000000 3.872983 2.236068

Solution: Here, seq() creates a sequence from -25 to 25 then abs() takes it’s
absolute value and sqrt() takes the square root.

9. Which one of the following is the correct output of the command seq(30,-30,
by = -10)?

a. [1] -30 -20 -10 0 10 20 30

b. [1] 0 10 20 30 -30 -20 -10

c. [1] -30 -20 -10 0 10 20 30

d. [1] 30 20 10 0 -10 -20 -30

Solution: Here, seq() creates a sequence from 30 to -30 with common difference of -
10 as defined.

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

seq(to = 150, length = 10)?

a. [1] 150 151 152 153 154 155 156 157 158 159

b. [1] 141 142 143 144 145 146 147 148 149 150

6
c. [1] 150 149 148 147 146 145 144 143 142 141

d. [1] 159 158 157 156 155 154 153 152 151 150

Solution: Here, seq() creates a sequence up-to 150 with integers and a total length
of 10

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

-seq(to = -150, length = 10)?

a. [1] 150 151 152 153 154 155 156 157 158 159

b. [1] 141 142 143 144 145 146 147 148 149 150

c. [1] 150 149 148 147 146 145 144 143 142 141

d. [1] 159 158 157 156 155 154 153 152 151 150

Solution: Here, seq() creates a sequence up-to -150 with integers and a total length
of 10, then take -ve of the sequence.

12. Which one of the following is the correct output of the command

seq(to = 111, length = 11, by = 11) ?

a. [1] 111 122 133 144 155 166 177 188 199 210 221

b. [1] 221 210 199 188 177 166 155 144 133 122 111

c. [1] 1 12 23 34 45 56 67 78 89 100 111


d. [1] 111 100 89 78 67 56 45 34 23 12 1

Solution: Here, seq() creates a sequence up-to 111 with a total length of 11 and
common difference of 11.

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

7
seq(from = -6, length = 6, by = -0.6) ?

a. [1] -3.0 -3.6 -4.2 -4.8 -5.4 -6.0

b. [1] -6.0 -6.6 -7.2 -7.8 -8.4 -9.0

c. [1] 3.0 3.6 4.2 4.8 5.4 6.0

d. [1] 6.0 6.6 7.2 7.8 8.4 9.0

Solution: Here, seq() creates a sequence of size 6, from -6 with a common difference
of -0.6.

14. Which one of the following is the correct output of Y for the following commands

X = c(25, 77, 35, 140, 8, 20, 120, 56, 19)


Y = seq(along = X) ?

a. [1] 1 2 3 4 5 6 7 8 9

b. [1] 7 6 3 8 1 9 4 5 2

c. [1] 2 5 4 9 1 8 3 6 7

d. [1] 9 8 7 6 5 4 3 2 1

Solution: Here, seq() creates a sequence of first +ve integers with a size same as
number of elements in X.

15. Which one of the following is the correct output of Y[X[4]] and Y[X[8]]
for the command

Y = c(25, 77, 35, 140, 8, 20, 120, 56, 19)

X = c(5, 7, 1, 4, 6, 3, 2, 9, 8) ?

a. 4 and 9 respectively.

b. 4 and 9 respectively.

8
c. NA and NA respectively.

d. 140 and 19 respectively.

Solution: Here, since X[4] is 4, Y[X[4]] is same as Y[4] which is 140. Similarly Y[X[8]]
is 19.

16. Which one of the following is the correct outcome of the command x[(x>45)]
where
x = c(34, 154, 176, 43, 88, 92, 37, 65, 59, 26, 38, 74, 66) ?

a. [1] 154 176 88 92 65 59 74 66

b. [1] 34 43 37 26 38

c. [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE

d. [1] FALSE FALSE FALSE FALSE FALSE

Solution: Here, x[(x>45)] provides a subsequence of x with elements greater than 45.

17. Which one of the following is the correct outcome of the command
x[(x - 120 > 30)] where
x = c(34, 154, 176, 43, 88, 92, 37, 65, 59, 26, 38, 74, 66)?

a. [1] 154 176 88 92 65 59 74 66

b. [1] 154 176

c. [1] NULL

d. [1] TRUE TRUE TRUE TRUE TRUE TRUE

Solution: Here, x[(x - 120 > 30)] provides a subsequence of x with elements
satisfying x-120>30.

9
18. Which one of the following is the correct outcome of the command
x[(x^2 - 5*x > 1000)] where
x = c(20, 65, 27, 55, 38, 37, 18, 52, 160, 237, 170, 61, 88,
52, 49, 12, 4, 24, 18, 22) ?

a. [1] 34 26

b. [1] TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE

c. [1] 65 55 38 37 52 160 237 170 61 88 52 49

d. [1] NULL

Solution: Here, x[(x^2 - 5*x > 1000)] provides a subsequence of x with elements
satisfying x^2 - 5*x > 1000.

19. If y = 25:35 then which one of the following is the correct outcome of the
command y[ - (2:8)] ?

a. [1] 26 27 28 29 30 31 32

b. [1] 25 33 34 35

c. [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE

d. [1] NULL

Solution: Here, y[-(2:8)] provides a subsequence of y with elements excluding #2 to


#8.

20. If y = 25:35 then which one of the following is the correct outcome of the
command y[(2:8)] ?

10
a. [1] 26 27 28 29 30 31 32

b. [1] 25 33 34 35

c. [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE

d. [1] NULL

Solution: Here, y[(2:8)] provides a subsequence of y with elements including #2 to


#8.

11
MOOC Course - Foundations of R Software

Answers of Assignment 6
1. c

2. b

3. c

4. a

5. d

6. b

7. a

8. b

9. d

10. b

11. d

12. c

13. b

14. a

15. d

16. a

17. b

18. c

19. b

20. a

12

You might also like