This set of R Programming Language Multiple Choice Questions & Answers (MCQs) focuses on “Vectorized Operations – 1”.
1. Which of the following is example of vectorized operation as far as subtraction is concerned?
> x <- 1:4 > y <- 6:9
a) x+y
b) x-y
c) x/y
d) x–y
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: This is simplest example of adding two vectors together.
4. What will be the output of the following R code?
> x <- 1:4 > x > 2
a) 1 2 3 4
b) FALSE FALSE TRUE TRUE
c) 1 2 3 4 5
d) 5 4 3 1 2 1
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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”))
View Answer
Explanation: 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”
View Answer
Explanation: 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”
View Answer
Explanation: Times can be coerced from a character string using the as.POSIXlt or as.POSIXct function.
Sanfoundry Global Education & Learning Series – R Programming Language.
Here’s the list of Best Books in R Programming Language.
- Check Information Technology Books
- Apply for Programming Internship
- Practice Programming MCQs
- Apply for R Programming Internship
- Check R Programming Books