MCQs on Working with Time and Dates | Ruby

Understanding how Ruby handles time and dates is essential for efficient programming. These MCQs will test your knowledge on Time and Date classes, formatting, parsing, and performing date/time calculations in Ruby.


1. Time and Date Classes

  1. Which class in Ruby is used to represent a specific point in time?
    • A) Date
    • B) Time
    • C) DateTime
    • D) Timestamp
  2. What does the Date class in Ruby represent?
    • A) A specific time with date and time details
    • B) Only the time portion of the date
    • C) Only the date, excluding the time
    • D) A time zone
  3. Which of the following is true about the Time class in Ruby?
    • A) It includes both the date and the time information
    • B) It only handles the date information
    • C) It handles only time zones
    • D) It is used for precise time measurement
  4. How can you create a new Date object for the current date in Ruby?
    • A) Date.now
    • B) Date.today
    • C) Date.current
    • D) Date.new
  5. Which of the following methods is used to get the current time in Ruby?
    • A) Time.now
    • B) Time.current
    • C) Time.new
    • D) Time.today

2. Formatting and Parsing Dates

  1. How do you format a Date object to display in a custom format in Ruby?
    • A) Date.to_s(format)
    • B) Date.strftime(format)
    • C) Date.format(format)
    • D) Date.custom_format(format)
  2. What is the correct way to parse a date string into a Date object in Ruby?
    • A) Date.parse("2024-11-19")
    • B) Date.strptime("2024-11-19")
    • C) Date.create("2024-11-19")
    • D) Date.new("2024-11-19")
  3. Which format directive in strftime represents the full month name in Ruby?
    • A) %m
    • B) %b
    • C) %B
    • D) %d
  4. Which method can be used to convert a Time object to a string in a specific format?
    • A) Time.to_s(format)
    • B) Time.strftime(format)
    • C) Time.custom_format(format)
    • D) Time.format(format)
  5. What would be the output of Time.now.strftime("%Y-%m-%d %H:%M:%S")?
    • A) Current time in YYYY-MM-DD HH:MM:SS format
    • B) Current time in DD-MM-YYYY HH:MM:SS format
    • C) Current time in MM-DD-YYYY HH:MM:SS format
    • D) Current time in HH:MM:SS YYYY-MM-DD format

3. Performing Date and Time Calculations

  1. How do you add 5 days to a Date object in Ruby?
    • A) date + 5
    • B) date.add(5)
    • C) date.days(5)
    • D) date.plus(5)
  2. Which method in Ruby is used to calculate the difference between two Time objects?
    • A) Time.diff
    • B) Time.subtract
    • C) Time.ago
    • D) Time - Time
  3. What is the result of Time.now - Time.now in Ruby?
    • A) The current time
    • B) 0
    • C) The time difference in seconds
    • D) An error
  4. How do you get the number of days between two Date objects in Ruby?
    • A) date1.days_between(date2)
    • B) date1 - date2
    • C) date1.diff(date2)
    • D) date1.days_until(date2)
  5. Which method in Ruby allows you to calculate the number of seconds from the current time to a specific Time object?
    • A) Time.seconds_to
    • B) Time.diff_seconds
    • C) Time - Time
    • D) Time.seconds_since

4. Advanced Date and Time Calculations

  1. How do you subtract months from a Date object in Ruby?
    • A) date - 3.months
    • B) date.months_ago(3)
    • C) date.subtract(3.months)
    • D) date.minus(3.months)
  2. What is the method used to calculate the beginning of the next month in Ruby?
    • A) date.next_month_begin
    • B) date.next_month
    • C) date.next_month_start
    • D) date.beginning_of_next_month
  3. In Ruby, which method would you use to get the start of the current week?
    • A) Time.start_of_week
    • B) Time.beginning_of_week
    • C) Time.current_week_start
    • D) Time.now.beginning_of_week
  4. What is the default time zone for Time.now in Ruby?
    • A) UTC
    • B) GMT
    • C) System’s local time zone
    • D) PST
  5. How can you set a specific time zone for Time.now in Ruby?
    • A) Time.now.in_time_zone('PST')
    • B) Time.now.set_time_zone('PST')
    • C) Time.now.timezone('PST')
    • D) Time.now.set_zone('PST')

5. Time Zones and Time Manipulation

  1. How do you convert a Time object to UTC in Ruby?
    • A) time.utc
    • B) time.to_utc
    • C) time.convert_to_utc
    • D) time.in_utc
  2. Which method is used to check if a Time object is in the UTC time zone in Ruby?
    • A) time.utc?
    • B) time.is_utc?
    • C) time.in_utc?
    • D) time.timezone == 'UTC'
  3. What would be the output of Time.now.utc? in Ruby?
    • A) true if the current time is in UTC
    • B) false if the current time is not in UTC
    • C) true if the time is converted to UTC
    • D) false if the time is converted to local time
  4. How do you add 2 hours to a Time object in Ruby?
    • A) time + 2.hours
    • B) time.add(2.hours)
    • C) time.plus(2.hours)
    • D) time + (2 * 60 * 60)
  5. How do you convert a Time object to the local time zone in Ruby?
    • A) time.localtime
    • B) time.in_local_time
    • C) time.to_local
    • D) time.convert_to_local

6. Time and Date Validation

  1. Which method is used to check if a given string represents a valid date in Ruby?
    • A) Date.valid?
    • B) Date.valid_date?
    • C) Date.parse
    • D) Date.valid_string?
  2. How do you check if a Time object represents a future date in Ruby?
    • A) time.is_future?
    • B) time.future?
    • C) time > Time.now
    • D) time.is_after_now?
  3. What does Date.valid_date? do in Ruby?
    • A) It returns true if the date is valid, false otherwise
    • B) It parses the date string into a valid Date object
    • C) It throws an exception if the date is invalid
    • D) It returns nil if the date is invalid
  4. How do you check if a Date object represents a leap year in Ruby?
    • A) date.leap_year?
    • B) date.is_leap_year?
    • C) date.year_is_leap?
    • D) date.is_leap?
  5. How do you validate if a time is valid within a specific range in Ruby?
    • A) time.between?(start_time, end_time)
    • B) time.is_between?(start_time, end_time)
    • C) time.validate_range(start_time, end_time)
    • D) time.check_range(start_time, end_time)

Answer Table

QnoAnswer (Option with text)
1B) Time
2C) Only the date, excluding the time
3A) It includes both the date and the time information
4B) Date.today
5A) Time.now
6B) Date.strftime(format)
7A) Date.parse(“2024-11-19”)
8C) %B
9B) Time.strftime(format)
10A) Current time in YYYY-MM-DD HH:MM:SS format
11A) date + 5
12D) Time – Time
13B) 0
14B) date1 – date2
15C) Time – Time
16B) date.months_ago(3)
17D) date.beginning_of_next_month
18D) Time.now.beginning_of_week
19C) System’s local time zone
20A) Time.now.in_time_zone(‘PST’)
21A) time.utc
22A) time.utc?
23A) true if the current time is in UTC
24A) time + 2.hours
25A) time.localtime
26B) Date.valid_date?
27C) time > Time.now
28A) It returns true if the date is valid, false otherwise
29A) date.leap_year?
30A) time.between?(start_time, end_time)

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