R Programming Language Multiple Choice Questions on “Vectorized Operations ”.
1. Which of the following is example of vectorized operation as far as subtraction is concerned?
a) x+y
b) x-y
c) x/y
d) x–y
Answer: b
Clarification: Subtraction, multiplication and division are also vectorized.
2. Point out the wrong statement?
a) Very less operations in R are vectorized
b) Vectorization allows you to write code that is efficient, concise, and easier to read than in non-vectorized languages
c) vectorized means that operations occur in parallel in certain R objects
d) Matrix operations are also vectorized
Answer: a
Clarification: Many operations in R are vectorized.
3. What will be the output of the following R code?
> x <- 1:4 > y <- 6:9 > z <- x + y > z
a) 7 9 11 13
b) 7 9 11 13 14
c) 9 7 11 13
d) NULL
Answer: a
Clarification: This is simplest example of adding two vectors together.
4. What will be the output of the following R code?
a) 1 2 3 4
b) FALSE FALSE TRUE TRUE
c) 1 2 3 4 5
d) 5 4 3 1 2 1
Answer: b
Clarification: Another operation you can do in a vectorized manner is logical comparisons.
5. Point out the wrong statement?
a) Dates are represented by the Date class
b) Times are represented by the POSIXct or the POSIXlt class
c) Dates are represented by the DateTime class
d) Times can be coerced from a character string
Answer: c
Clarification: Dates are stored internally as the number of days since 1970-01-01 while times are stored internally as the number of seconds since 1970-01-01.
6. What will be the output of the following R code?
> x <- 1:4 > y <- 6:9 > x/y
a) 0.1666667 0.2857143 0.4444444
b) 0.1666667 0.2857143 0.3750000 0.4444444
c) 0.2857143 0.3750000 0.4444444
d) Error
Answer: b
Clarification: Logical operations return a logical vector of TRUE and FALSE.
7. What will be the output of the following R code?
> x <- matrix(1:4, 2, 2) > y <- matrix(rep(10, 4), 2, 2) > x * y
a)
[,1] [,2] [1,] 10 30 [2,] 20 40
b)
[,1] [,2] [1,] 10 30 [2,] 30 40
c)
[,1] [,2] [1,] 20 30 [2,] 20 40
d) Error
Answer: a
Clarification: Matrix operations are also vectorized, making for nicely compact notation.
8. Which of the following code represents internal representation of a Date object?
a) class(as.Date(“1970-01-02”))
b) unclass(as.Date(“1970-01-02”))
c) unclassint(as.Date(“1970-01-02”))
d) classint(as.Date(“1970-02-02”))
Answer: b
Clarification: You can see the internal representation of a Date object by using the unclass() function.
9. What will be the output of the following R code?
> x <- as.Date("1970-01-01") > x
a) “1970-01-01”
b) “1970-01-02”
c) “1970-02-01”
d) “1970-02-02”
Answer: a
Clarification: Dates are represented by the Date class and can be coerced from a character string using the as.Date() function.
10. What will be the output of the following R code?
> x <- Sys.time() > class(x)
a) “POSIXct” “POSIXt”
b) “POSIXXt” “POSIXt”
c) “POSIXct” “POSIct”
d) “POSIXt” “POSIXt”
Answer: a
Clarification: Times can be coerced from a character string using the as.POSIXlt or as.POSIXct function.