MCQs on Working with Dates and Times | Groovy

Groovy offers robust support for working with dates and times, making it easier to perform date manipulations, extend date functionalities, format and parse dates, and calculate time differences. This guide will help you explore Groovy’s features for efficient handling of date and time operations in your applications.


1. Date and Time Manipulations

  1. What method in Groovy is used to get the current date and time?
    • a) new Date()
    • b) Date.now()
    • c) Date.today()
    • d) new DateTime()
  2. How do you create a Date object for a specific date, like January 1, 2024, in Groovy?
    • a) new Date('2024-01-01')
    • b) Date.parse('2024-01-01')
    • c) new Date(2024, 01, 01)
    • d) new Date('01/01/2024')
  3. How do you add days to a Groovy Date object?
    • a) date.addDays(5)
    • b) date.plus(5, 'days')
    • c) date.add(5, 'days')
    • d) date.add(5)
  4. Which method would you use to subtract days from a Date object in Groovy?
    • a) date.minus(5, 'days')
    • b) date.subtract(5)
    • c) date.decrement(5)
    • d) date.remove(5)
  5. Which of the following can be used to get the day of the week from a Date in Groovy?
    • a) date.getDayOfWeek()
    • b) date.dayOfWeek()
    • c) date.dayOfTheWeek()
    • d) date.getDay()
  6. How do you compare two dates in Groovy to check if one is earlier than the other?
    • a) date1.before(date2)
    • b) date1.earlierThan(date2)
    • c) date1.compare(date2)
    • d) date1.isBefore(date2)
  7. Which method is used to check if a Date object represents today’s date in Groovy?
    • a) date.today()
    • b) date.isToday()
    • c) date.isTodayDate()
    • d) date.equals(new Date())
  8. What is the default format of the Date object when printed in Groovy?
    • a) yyyy-MM-dd
    • b) MM/dd/yyyy
    • c) E, dd MMM yyyy HH:mm:ss z
    • d) dd/MM/yyyy
  9. How can you convert a Date object into a String in Groovy?
    • a) date.toString()
    • b) date.toDateString()
    • c) date.convertToString()
    • d) date.format()
  10. Which class is commonly used in Groovy to perform date and time calculations?
    • a) DateTime
    • b) DateDuration
    • c) LocalDateTime
    • d) TimeUtils

2. Groovy Date Extensions

  1. What Groovy feature allows you to perform additional operations on Date objects like adding months?
    • a) GroovyDateExtensions
    • b) GroovyTime
    • c) DatePlusExtension
    • d) GroovyDateUtils
  2. Which method can be used to add a month to a Date object in Groovy using Date Extensions?
    • a) date.plus(1, 'month')
    • b) date.addMonths(1)
    • c) date.add(1, 'month')
    • d) date.plusMonth(1)
  3. What will the following code do in Groovy: new Date() + 10?
    • a) Adds 10 days to the current date
    • b) Adds 10 months to the current date
    • c) Adds 10 hours to the current date
    • d) Adds 10 years to the current date
  4. Which of the following methods will subtract a specific amount of days using Groovy Date extensions?
    • a) date - 5
    • b) date.minus(5, 'days')
    • c) date.subtract(5)
    • d) date.removeDays(5)
  5. What would be the result of new Date() + 1.month in Groovy?
    • a) Adds 1 month to the current date
    • b) Adds 1 day to the current date
    • c) Adds 1 year to the current date
    • d) Adds 1 hour to the current date
  6. Which method in Groovy allows for easy date manipulation using Groovy Date Extensions?
    • a) plus()
    • b) minus()
    • c) add()
    • d) timeShift()
  7. What is returned by new Date() + 3.days in Groovy?
    • a) Current date plus 3 days
    • b) Current date plus 3 hours
    • c) Current date plus 3 months
    • d) Current date plus 3 years
  8. How can you get the last day of the current month in Groovy?
    • a) new Date().lastDayOfMonth()
    • b) new Date().endOfMonth()
    • c) new Date().monthEnd()
    • d) new Date().lastDayOfCurrentMonth()
  9. How would you subtract one hour from a Date object using Groovy Date Extensions?
    • a) date - 1.hours
    • b) date.minus(1, 'hour')
    • c) date.subtract(1, 'hour')
    • d) date.remove(1, 'hour')
  10. What does the method new Date().nextMonth() return in Groovy?
    • a) The first day of the next month
    • b) The current date plus one month
    • c) The current month’s end date
    • d) The last day of the next month

3. Formatting and Parsing Dates

  1. How do you format a Date object into a string in a specific format in Groovy?
    • a) date.format('dd-MM-yyyy')
    • b) date.toFormattedString('dd-MM-yyyy')
    • c) date.toString('dd-MM-yyyy')
    • d) date.convert('dd-MM-yyyy')
  2. What is the correct way to parse a date string into a Date object in Groovy?
    • a) Date.parse('yyyy-MM-dd', '2024-01-01')
    • b) Date.parse('01-01-2024')
    • c) Date.parse('yyyy/MM/dd')
    • d) Date.fromString('2024-01-01')
  3. Which method is used to parse a date with a custom format in Groovy?
    • a) Date.parse()
    • b) Date.format()
    • c) Date.convert()
    • d) Date.fromString()
  4. How can you parse a date string from the format MM-dd-yyyy in Groovy?
    • a) Date.parse('MM-dd-yyyy', '12-25-2024')
    • b) Date.format('MM-dd-yyyy', '12-25-2024')
    • c) Date.convert('12-25-2024')
    • d) Date.from('MM-dd-yyyy', '12-25-2024')
  5. Which of the following is a valid date format pattern in Groovy?
    • a) yyyy/MM/dd
    • b) dd.MM.yyyy
    • c) dd-MM-yyyy
    • d) All of the above
  6. How would you format the current date to show the day, month, and year in Groovy?
    • a) new Date().format('dd-MM-yyyy')
    • b) new Date().toString('MM-dd-yyyy')
    • c) new Date().format('yyyy/dd/MM')
    • d) new Date().format('yyyy-MM-dd')
  7. How can you format a date to show the day of the week and time in Groovy?
    • a) new Date().format('EEE, hh:mm:ss a')
    • b) new Date().toString('EEE, hh:mm:ss a')
    • c) new Date().format('hh:mm:ss')
    • d) new Date().toFormattedString('EEE, hh:mm:ss a')
  8. What is the method used in Groovy to parse a date from a string in the format yyyy/MM/dd?
    • a) Date.parse('yyyy/MM/dd', '2024/01/01')
    • b) Date.format('yyyy/MM/dd')
    • c) Date.fromString('2024/01/01')
    • d) Date.convert('yyyy/MM/dd', '2024/01/01')
  9. Which of the following format specifiers is used for the year in Groovy date formatting?
    • a) YYYY
    • b) yyyy
    • c) YY
    • d) YY
  10. How can you convert a date string back to a Date object after parsing in Groovy?
    • a) Date.parse()
    • b) Date.fromString()
    • c) Date.format()
    • d) Date.convert()

Answers

QnoAnswer
1a) new Date()
2c) new Date(2024, 01, 01)
3b) date.plus(5, 'days')
4a) date.minus(5, 'days')
5a) date.getDayOfWeek()
6a) date1.before(date2)
7b) date.isToday()
8c) E, dd MMM yyyy HH:mm:ss z
9a) date.toString()
10a) DateTime
11a) GroovyDateExtensions
12a) date.plus(1, 'month')
13a) Adds 10 days to the current date
14a) date - 5
15a) Adds 1 month to the current date
16a) plus()
17a) Current date plus 3 days
18a) new Date().lastDayOfMonth()
19a) date - 1.hours
20a) The first day of the next month
21a) date.format('dd-MM-yyyy')
22a) Date.parse('yyyy-MM-dd', '2024-01-01')
23a) Date.parse()
24a) Date.parse('MM-dd-yyyy', '12-25-2024')
25d) All of the above
26a) new Date().format('dd-MM-yyyy')
27a) new Date().format('EEE, hh:mm:ss a')
28a) Date.parse('yyyy/MM/dd', '2024/01/01')
29b) yyyy
30a) Date.parse()

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