30+ MCQs on Python Date and Time Handling

Ayushi Trivedi Last Updated : 06 Mar, 2024
7 min read

Welcome to the Python Date and Time Handling Python Interview Questions! Working with dates and times is a common task in programming, and Python provides powerful modules such as datetime and time to handle date and time-related operations. These questions will test your understanding of how to work with dates, times, and datetime objects in Python, including formatting, arithmetic operations, timezones, and more. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Let’s explore the world of Python date and time handling together!

Python Date and Time Handling

30+ Python Interview Questions on Python Date and Time Handling

Q1. Which module in Python provides classes for manipulating dates and times?

a) datetime

b) calendar

c) time

d) dateutil

Answer: a

Explanation: The datetime module in Python provides classes for manipulating dates and times.

Q2. Which method is used to get the current local date and time in Python datetime module?

a) current_time()

b) today()

c) now()

d) get_current_time()

Answer: c

Explanation: The now() method from the datetime module is used to get the current local date and time.

Q3. Which of the following formats is used to represent dates in Python’s datetime module?

a) YYYY-MM-DD HH:MM:SS

b) MM/DD/YYYY HH:MM:SS

c) DD-MM-YYYY HH:MM:SS

d) HH:MM:SS DD/MM/YYYY

Answer: a

Explanation: Dates are typically represented in the format YYYY-MM-DD HH:MM:SS in Python’s datetime module.

Q4. What does the strftime() function do in Python datetime module?

a) Parses a string into a datetime object

b) Formats a datetime object as a string

c) Calculates the difference between two datetime objects

d) Returns the current timestamp

Answer: b

Explanation: The strftime() function in Python datetime module is used to format a datetime object as a string.

Q5. Which method is used to convert a string to a datetime object in Python datetime module?

a) strptime()

b) parse()

c) from_string()

d) convert()

Answer: a

Explanation: The strptime() method is used to convert a string to a datetime object in Python datetime module.

Q6. What is the result of datetime.datetime.now().isoformat()?

a) Current local date and time in ISO format

b) Current UTC date and time in ISO format

c) Current local date and time as a string

d) Current UTC date and time as a string

Answer: a

Explanation: datetime.datetime.now().isoformat() returns the current local date and time in ISO format, such as “2023-05-18T12:30:00”.

Q7. Which method is used to add a specified number of days to a datetime object?

a) add_days()

b) days_added()

c) add_timedelta()

d) timedelta()

Answer: d

Explanation: The timedelta() method is used to add a specified number of days to a datetime object.

Q8. What is the result of datetime.datetime(2023, 12, 31).strftime("%Y-%m-%d")?

a) “31-12-2023”

b) “12-31-2023”

c) “2023-12-31”

d) “2023-31-12”

Answer: c

Explanation: datetime.datetime(2023, 12, 31).strftime("%Y-%m-%d") will result in “2023-12-31” because %Y represents the year, %m represents the month, and %d represents the day.

Q9. Which of the following format codes is used to represent the month as a zero-padded decimal number (01-12) in strftime()?

a) %M

b) %m

c) %d

d) %y

Answer: b

Explanation: The %m format code is used to represent the month as a zero-padded decimal number (01-12) in strftime().

Q10. What is the weekday index (0-6) of the datetime object datetime.datetime(2023, 9, 15)?

a) 0 (Monday)

b) 1 (Tuesday)

c) 4 (Friday)

d) 6 (Sunday)

Answer: c

Explanation: datetime.datetime(2023, 9, 15).weekday() will return 4, which corresponds to Friday (0 is Monday, 6 is Sunday).

Q11. Which of the following is NOT a valid method to create a datetime object in Python?

a) datetime.datetime(2023, 5, 15)

b) datetime.now()

c) datetime.strptime("2023-05-15", "%Y-%m-%d")

d) datetime.fromtimestamp(1620999600)

Answer: b

Explanation: There is no datetime.now() method in Python. Instead, it should be datetime.datetime.now().

Q12. What will datetime.timedelta(days=5) represent?

a) Time difference of 5 seconds

b) Time difference of 5 minutes

c) Time difference of 5 hours

d) Time difference of 5 days

Answer: d

Explanation: datetime.timedelta(days=5) represents a time difference of 5 days.

Q13. Which method is used to extract the year from a datetime object in Python?

a) year()

b) get_year()

c) datetime.year

d) datetime.year()

Answer: a

Explanation: The year() method is used to extract the year from a datetime object in Python.

Q14. What will be the output of datetime.datetime.now().date()?

a) Current local time

b) Current UTC date

c) Current local date

d) Current UTC time

Answer: c

Explanation: datetime.datetime.now().date() returns the current local date without the time component.

Q15. Which method is used to subtract one datetime object from another to get a time difference?

a) subtract()

b) difference()

c) time_diff()

d) timedelta()

Answer: d

Explanation: The timedelta() method is used to subtract one datetime object from another to get a time difference.

Q16. What does the weekday() method of a datetime object in Python return?

a) Day of the week as an integer (0-6)

b) Day of the month as an integer (1-31)

c) Day of the year as an integer (1-365)

d) Day of the week as a string (e.g., “Monday”)

Answer: a

Explanation: The weekday() method of a datetime object in Python returns the day of the week as an integer, where 0 is Monday and 6 is Sunday.

Q17. Which of the following will convert a string “2023-12-31” to a datetime object?

a) datetime.strptime("2023-12-31", "%Y-%m-%d")

b) datetime.convert("2023-12-31")

c) datetime.from_string("2023-12-31")

d) datetime.parse("2023-12-31")

Answer: a

Explanation: The strptime() method is used to convert a string to a datetime object by specifying the format.

Q18. What will datetime.datetime.now().time() return?

a) Current local date and time

b) Current UTC date and time

c) Current local time

d) Current UTC time

Answer: c

Explanation: datetime.datetime.now().time() returns the current local time without the date component.

Q19. How can you get the number of days in a month in Python datetime module?

a) Using the days_in_month() method

b) Using the monthrange() function

c) Using the get_days() function

d) Using the month_days() method

Answer: b

Explanation: The monthrange() function in Python datetime module can be used to get the number of days in a month.

Q20. Which of the following is NOT a valid format code for strftime() in Python?

a) %D

b) %B

c) %Y

d) %M

Answer: d

Explanation: %M is not a valid format code for strftime(). It represents minutes. The correct answer should be %I which represents hour (12-hour clock).

Q21. What will datetime.datetime(2023, 5, 18).strftime("%A") return?

a) “Monday”

b) “Tuesday”

c) “Wednesday”

d) “Thursday”

Answer: c

Explanation: datetime.datetime(2023, 5, 18).strftime("%A") will return “Wednesday” as May 18, 2023, is a Wednesday.

Q22. How do you convert a datetime object to a Unix timestamp in Python?

a) datetime.timestamp()

b) datetime.to_timestamp()

c) datetime.get_timestamp()

d) datetime.unix_timestamp()

Answer: a

Explanation: The timestamp() method of a datetime object is used to convert it to a Unix timestamp.

Q23. What does the replace() method of a datetime object in Python datetime module do?

a) Replaces the datetime object with a new one

b) Replaces the specified attribute of the datetime object with a new value

c) Replaces the date part of the datetime object with a new date

d) Replaces the time part of the datetime object with a new time

Answer: c

Explanation: The replace() method of a datetime object in Python datetime module replaces the date part of the datetime object with a new date.

Q24. Which method is used to check if a year is a leap year in Python datetime module?

a) is_leap_year()

b) leap_year()

c) check_leap_year()

d) datetime.leapyear()

Answer: a

Explanation: The is_leap_year() method is used to check if a year is a leap year in Python datetime module.

Q25. What does the ctime() method of a datetime object in Python datetime module do?

a) Returns the current date and time as a string

b) Returns the date and time in a human-readable format

c) Returns the date and time in a machine-readable format

d) Returns the current date and time as a Unix timestamp

Answer: b

Explanation: The ctime() method of a datetime object in Python datetime module returns the date and time in a human-readable format.

Q26. What will datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") return?

a) Current local date and time in ISO format

b) Current local date and time in “YYYY-MM-DD HH:MM:SS” format

c) Current UTC date and time in “YYYY-MM-DD HH:MM:SS” format

d) Current local date and time as a Unix timestamp

Answer: b

Explanation: datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") will return the current local date and time in the format “YYYY-MM-DD HH:MM:SS”.

Q27. How do you get the week number of the year from a datetime object in Python?

a) week_number()

b) get_week()

c) strftime("%W")

d) get_week_number()

Answer: c

Explanation: The %W format code in strftime() can be used to get the week number of the year from a datetime object.

Q28. Which of the following is a valid way to add 1 day to a datetime object my_date?

a) my_date + timedelta(days=1)

b) my_date.add_days(1)

c) my_date.add_day(1)

d) my_date.increment_days(1)

Answer: a

Explanation: The correct way to add 1 day to a datetime object my_date is my_date + timedelta(days=1).

Q29. What is the output of datetime.datetime.strptime("2023-12-31", "%Y-%m-%d").year?

a) 2023

b) 12

c) 31

d) “2023-12-31”

Answer: a

Explanation: datetime.datetime.strptime("2023-12-31", "%Y-%m-%d").year will output 2023 because strptime() converts the string “2023-12-31” to a datetime object, and .year extracts the year from it.

Q30. How can you calculate the difference between two datetime objects datetime1 and datetime2 in Python?

a) datetime1 - datetime2

b) timedelta(datetime1, datetime2)

c) datetime1.difference(datetime2)

d) timedelta(datetime2 - datetime1)

Answer: a

Explanation: The difference between two datetime objects datetime1 and datetime2 in Python is calculated using datetime1 - datetime2.

Q31. Which of the following methods is used to convert a datetime object to a string in Python datetime module?

a) to_string()

b) str()

c) format()

d) strftime()

Answer: d

Explanation: The strftime() method is used to convert a datetime object to a string with a specified format.

Q32. What will datetime.datetime.strptime("2023-05-18", "%Y-%m-%d").strftime("%B") return?

a) “May”

b) “January”

c) “August”

d) “December”

Answer: a

Explanation: datetime.datetime.strptime("2023-05-18", "%Y-%m-%d").strftime("%B") will return “May” as the full month name.

Q33. How can you get the day of the week as a string (e.g., “Monday”) from a datetime object in Python?

a) weekday_string()

b) get_weekday()

c) strftime("%A")

d) get_day_of_week()

Answer: c

Explanation: The %A format code in strftime() is used to get the full day of the week as a string.

Congratulations on completing the Python Date and Time Handling MCQs! Handling dates and times is an essential skill in programming, and Python offers a rich set of modules and functionalities for these tasks. By mastering date and time handling in Python, you gain the ability to work with timestamps, perform calculations, and format dates and times according to specific requirements. Keep practicing and experimenting with the datetime and time modules to become proficient in working with dates and times effectively. If you have any questions or want to delve deeper into any topic, don’t hesitate to continue your learning journey. Happy coding!

You can also enroll in our free Python Course Today!

Read our more articles related to MCQs in Python:

My name is Ayushi Trivedi. I am a B. Tech graduate. I have 3 years of experience working as an educator and content editor. I have worked with various python libraries, like numpy, pandas, seaborn, matplotlib, scikit, imblearn, linear regression and many more. I am also an author. My first book named #turning25 has been published and is available on amazon and flipkart. Here, I am technical content editor at Analytics Vidhya. I feel proud and happy to be AVian. I have a great team to work with. I love building the bridge between the technology and the learner.

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details