Python Question and Answers – Datetime Module – 1

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Datetime Module – 1”.

1. What will be the output of the following Python code?

import datetime
d=datetime.date(2016,7,24)
print(d)

a) Error
b) 2017-07-24
c) 2017-7-24
d) 24-7-2017
View Answer

Answer: b
Explanation: In the snippet of code shown above, we are simply printing the date entered by us. We enter the date in the format: yyyy,m,dd. The date is then printed in the format: yyyy-mm-dd. Hence the output is: 2017-07-24.
advertisement
advertisement

2. What will be the output of the following Python code?

import datetime
d=datetime.date(2017,06,18)
print(d)

a) Error
b) 2017-06-18
c) 18-06-2017
d) 06-18-2017
View Answer

Answer: a
Explanation: The code shown above will result in an error because of the format of the date entered. Had the date been entered as: d=datetime.date(2017,6,18), no error would have been thrown.

3. What will be the output of the following Python code if the system date is 18th August, 2016?

advertisement
tday=datetime.date.today()
print(tday.month())

a) August
b) Aug
c) 08
d) 8
View Answer

Answer: d
Explanation: The code shown above prints the month number from the system date. Therefor the output will be 8 if the system date is 18th August, 2016.
advertisement

4. What will be the output of the following Python code if the system date is 18th June, 2017 (Sunday)?

import datetime
tday=datetime.date.today()
print(tday)

a) 18-06-2017
b) 06-18-2017
c) 2017-06-18
d) Error
View Answer

Answer: c
Explanation: The code shown above prints the system date in the format yyyy-mm-dd. Hence the output of this code is: 2017-06-18.

5. What will be the output of the following Python code if the system date is 18th June, 2017 (Sunday)?

tday=datetime.date.today()
print(tday.weekday())

a) 6
b) 1
c) 0
d) 7
View Answer

Answer: a
Explanation: The code shown above prints an integer depending on which day of the week it is. Monday-0, Tuesday-1, Wednesday-2, Thursday-3, Friday-4, Saturday-5, Sunday-6. Hence the output is 6 in the case shown above.

6. What will be the output of the following Python code if the system date is 21st June, 2017 (Wednesday)?

tday=datetime.date.today()
print(tday.isoweekday())

a) Wed
b) Wednesday
c) 2
d) 3
View Answer

Answer: d
Explanation: This code prints an integer depending on which day of the week it is. Monday-1, Tuesday-2, Wednesday-3, Thursday-4, Friday-5, Saturday-6, Sunday-7. Hence the output of the code shown above is 3.

7. Point out the error (if any) in the code shown below if the system date is 18th June, 2017?

tday=datetime.date.today()
bday=datetime.date(2017,9,18)
till_bday=bday-tday
print(till_bday)

a) 3 months, 0:00:00
b) 90 days, 0:00:00
c) 3 months 2 days, 0:00:00
d) 92 days, 0:00:00
View Answer

Answer: d
Explanation: The code shown above can be used to find the number of days between two given dates. The output of the code shown above will thus be 92.

8. The value returned when we use the function isoweekday() is ______ and that for the function weekday() is ________ if the system date is 19th June, 2017 (Monday).
a) 0,0
b) 0,1
c) 1,0
d) 1,1
View Answer

Answer: c
Explanation: The value returned when we use the function isoweekday() is 1 and that for the function weekday() is 0 if the system date is 19th June, 2017 (Monday).

9. Which of the following will throw an error if used after the following Python code?

tday=datetime.date.today()
bday=datetime.date(2017,9,18)
t_day=bday-tday

a) print(t_day.seconds)
b) print(t_day.months)
c) print(t_day.max)
d) print(t_day.resolution)
View Answer

Answer: b
Explanation: The statement: print(t_day.months) will throw an error because there is no function such as t_day.months, whereas t_day.seconds, t_day.max and t_day.resolution are valid, provided that t_day is defined.

10. What will be the output of the following Python code if the system date is: 6/19/2017

tday=datetime.date.today()
tdelta=datetime.timedelta(days=10)
print(tday+tdelta)

a) 2017-16-19
b) 2017-06-9
c) 2017-06-29
d) Error
View Answer

Answer: c
Explanation: The code shown above will add the specified number of days to the current date and print the new date. On adding ten days to 6/19/2017, we get 6/29/2017. Hence the output is: 2017-06-29.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.