250+ TOP MCQs on Vectorized Operations and Answers

R Programming Interview Questions and Answers for experienced focuses on “Vectorized Operations”

1. Which of the following function gives the day of the week?
a) weekdays
b) months
c) quarters
d) semesters

Answer: a
Clarification: Weekdays will give the day of the week. months function give the month name. Quarters divide the year into fourths. Semesters divide the year into halfs.

2. Point out the correct statement?
a) Times use the POSIXct and POSIXlt class
b) Dates and times have special classes in R that allow for numerical and statistical calculations
c) Character strings can be coerced to Date/Time classes using the strptime function
d) All of the mentioned

Answer: d
Clarification: Character strings can be coerced to Date/Time classes using the as.Date, as.POSIXlt, or as.POSIXct.

3. What will be the output of the following R code?

> p <- as.POSIXlt(x)
> names(unclass(p))
> p$wday

a) 1
b) 2
c) 3
d) NULL

Answer: a
Clarification: The POSIXlt object contains some useful metadata.

4. What will be the output of the following R code?

> datestring <- c("January 10, 2012 10:40", "December 9, 2011 9:10")
> x <- strptime(datestring, "%B %d, %Y %H:%M")
> x

a) “2012-01-10 10:40:00 EST” “2011-12-09 09:10:00 EST”
b) “2012-01-10 10:40:00 IST” “2011-12-09 09:10:00 IST”
c) “2012-01-10 10:40:00 GMT” “2011-12-09 09:10:00 GMT”
d) Error

Answer: a
Clarification: strptime() takes a character vector that has dates and times and converts them into to a POSIXlt object.

5. Point out the wrong statement?
a) POSIXct is just a very large integer under the hood
b) POSIXlt stores a bunch of other useful information like the day of the week, day of the year, month, day of the month
c) There are a number of generic functions that work on dates and times to help you extract pieces of dates and/or times
d) None of the mentioned

Answer: d
Clarification: POSIXct uses a useful class when you want to store times in something like a data frame.

6. What will be the output of the following R code?

> x <- as.Date("2012-01-01")
> y <- strptime("9 Jan 2011 11:34:21", "%d %b %Y %H:%M:%S")
> x-y

a) Time difference of 356.3095 days
b) Warning
c) NULL
d) Error

Answer: b
Clarification: You can use mathematical operations on dates and times.

7. What will be the output of the following R code?

> x <- as.Date("2012-03-01")
> y <- as.Date("2012-02-28")
> x-y

a) Time difference of 3 days
b) Time difference of 2 days
c) Time difference of 1 days
d) Time difference of 5 days

Answer: b
Clarification: Matrix operations are also vectorized, making for nicely compact notation.

8. What will be the output of the following R code?

> x <- as.POSIXct("2012-10-25 01:00:00")
> y <- as.POSIXct("2012-10-25 06:00:00", tz = "GMT")
> y-x

a) Time difference of 1 hour
b) Time difference of 1 min
c) Time difference of 1 sec
d) Time difference of 5 sec

Answer: a
Clarification: POSIXct is just a very large integer under the hood.

Leave a Reply

Your email address will not be published. Required fields are marked *