MCQs on Working with Dates and Times | R

Working with dates and times is crucial in R for data analysis. Understanding date-time classes like Date and POSIXct, parsing and formatting dates, and handling time zone calculations is essential for accurate temporal data manipulation.


1. Date-Time Classes in R (Date, POSIXct)

  1. Which function is used to create a Date object in R?
    a) as.Date()
    b) date()
    c) to.Date()
    d) create.Date()
  2. What does the POSIXct class represent in R?
    a) A date without a time component
    b) A date-time object that includes a time zone
    c) A time without a date component
    d) A date-time object with fractional seconds
  3. Which of the following functions returns a POSIXct object?
    a) as.POSIXct()
    b) as.Date()
    c) POSIXct()
    d) time.POSIX()
  4. What is the format used by the Date class in R?
    a) YYYY-MM-DD
    b) MM-DD-YYYY
    c) DD-MM-YYYY
    d) YYYY/DD/MM
  5. How can you convert a Date object to POSIXct in R?
    a) as.POSIXct()
    b) as.Date()
    c) as.POSIXlt()
    d) as.Date.POSIXct()
  6. Which function in R returns the current system date?
    a) Sys.Date()
    b) currentDate()
    c) getDate()
    d) today()
  7. How do you extract the time component from a POSIXct object?
    a) format()
    b) time()
    c) hours()
    d) POSIXtime()
  8. What is the default format for displaying POSIXct objects in R?
    a) “yyyy-mm-dd”
    b) “mm/dd/yyyy”
    c) “YYYY-MM-DD hh:mm”
    d) “hh:mm”
  9. Which function is used to extract the date part from a POSIXct object?
    a) as.Date()
    b) date()
    c) extractDate()
    d) getDate()
  10. How can you check the class of a Date object in R?
    a) class()
    b) type()
    c) objectClass()
    d) typeof()

2. Parsing and Formatting Dates

  1. Which function is used to parse a string into a Date object in R?
    a) as.Date()
    b) strptime()
    c) parseDate()
    d) dateParser()
  2. What is the default date format in the as.Date() function?
    a) YYYY/MM/DD
    b) MM-DD-YYYY
    c) YYYY-MM-DD
    d) DD/MM/YYYY
  3. How can you specify a custom format when parsing a date in R?
    a) Use the format argument in as.Date()
    b) Use the format() function after parsing
    c) Use the strptime() function
    d) Use as.Date() with the customFormat argument
  4. In R, which of the following represents the day of the month in a date format?
    a) %d
    b) %m
    c) %Y
    d) %h
  5. How do you format a Date object into a character string in R?
    a) as.character()
    b) format()
    c) toString()
    d) toDate()
  6. Which function is used to parse a string representing a date and time into a POSIXct object?
    a) as.POSIXct()
    b) strptime()
    c) as.POSIXlt()
    d) parseDatetime()
  7. How do you include a timezone when formatting a Date object in R?
    a) Use the tz argument in format()
    b) Use Sys.timezone()
    c) Use format() with timezone argument
    d) Include %Z in the format string
  8. What does the %Y format represent in R when working with dates?
    a) Day of the week
    b) Month
    c) Year with century (e.g., 2024)
    d) Day of the year
  9. How would you format a date in R to display “March 12, 2024”?
    a) format(date, “%d %b %Y”)
    b) format(date, “%b %d, %Y”)
    c) format(date, “%B %d, %Y”)
    d) format(date, “%d %m, %Y”)
  10. Which function can be used to convert a string with a date and time into a POSIXct object?
    a) as.POSIXlt()
    b) strptime()
    c) as.Date()
    d) dateToPosixct()

3. Calculations with Dates and Time Zones

  1. How can you perform date arithmetic in R?
    a) Using the difftime() function
    b) Using arithmetic operators directly with Date objects
    c) Using the dateAdd() function
    d) Both a and b
  2. How do you subtract two Date objects in R?
    a) Use difftime()
    b) Use dateDiff()
    c) Use simple subtraction (-)
    d) Both a and b
  3. What does the Sys.time() function return in R?
    a) The current system time as a Date object
    b) The current system time with timezone information as a POSIXct object
    c) The current system date
    d) The current system time without timezone
  4. How can you add days to a Date object in R?
    a) Using + operator with a numeric value
    b) Using dateAdd()
    c) Using addDays()
    d) Using add()
  5. Which function is used to get the current system time with timezone information in R?
    a) Sys.time()
    b) systemTime()
    c) currentTime()
    d) time()
  6. How can you get the difference between two dates in days?
    a) dateDiff()
    b) difftime()
    c) dateDifference()
    d) timeDifference()
  7. How can you convert a Date object to another time zone in R?
    a) Use with_tz() from the lubridate package
    b) Use format()
    c) Use as.POSIXct()
    d) Use as.Date()
  8. Which function in R helps in converting a Date object from one time zone to another?
    a) as.POSIXlt()
    b) with_tz()
    c) changeTimezone()
    d) timeZone()
  9. How do you get the current system timezone in R?
    a) Sys.timezone()
    b) getTimeZone()
    c) currentTimeZone()
    d) timezone()
  10. Which class in R supports time zone-aware objects?
    a) Date
    b) POSIXct
    c) Time
    d) Timestamp

Answers Table

QNoAnswer (Option with Text)
1a) as.Date()
2b) A date-time object that includes a time zone
3a) as.POSIXct()
4a) YYYY-MM-DD
5a) as.POSIXct()
6a) Sys.Date()
7a) format()
8c) “YYYY-MM-DD hh:mm”
9a) as.Date()
10a) class()
11b) strptime()
12c) YYYY-MM-DD
13c) Use the strptime() function
14a) %d
15b) format()
16b) strptime()
17a) Use the tz argument in format()
18c) Year with century (e.g., 2024)
19c) format(date, “%B %d, %Y”)
20b) strptime()
21d) Both a and b
22c) Use simple subtraction (-)
23b) The current system time with timezone information as a POSIXct object
24a) Using + operator with a numeric value
25a) Sys.time()
26b) difftime()
27a) Use with_tz() from the lubridate package
28b) with_tz()
29a) Sys.timezone()
30b) POSIXct

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top