Professional Documents
Culture Documents
Assignment6 L22-L25
Assignment6 L22-L25
Assignment6 L22-L25
July 2023
Assignment 6
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.
a.
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
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
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.
abs(seq(-40,-45))?
a. [1] 40 41 42 43 44 45
c. [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))) ?
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)?
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
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
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
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
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) ?
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
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
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.
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) ?
b. [1] 34 43 37 26 38
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)?
c. [1] NULL
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
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
d. [1] NULL
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
d. [1] NULL
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