Python Question and Answers – Datetime Module – 2

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

1. The output of both of the print statements is the same.

import datetime
dt_1 = datetime.datetime.today()
dt_2 = datetime.datetime.now()
print(dt_1)
print(dt_2)

a) True
b) False
View Answer

Answer: b
Explanation: The output of the two print statements is not the same because of the difference in time between the execution of the two print statements. There is a difference in the order of milliseconds between the two statements and this is reflected in the output.
advertisement
advertisement

2. Which of the following functions can be used to find the coordinated universal time, assuming that the datetime module has already been imported?
a) datetime.utc()
b) datetime.datetime.utc()
c) datetime.utcnow()
d) datetime.datetime.utcnow()
View Answer

Answer: d
Explanation: The function datetime.datetime.utcnow() can be used to find the UTC (Coordinated Universal Time), assuming that the datetime module has already been imported. The other function s shown above are invalid.

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

Note: Join free Sanfoundry classes at Telegram or Youtube
import time
time.time()

a) The number of hours passed since 1st January, 1970
b) The number of days passed since 1st January, 1970
c) The number of seconds passed since 1st January, 1970
d) The number of minutes passed since 1st January, 1970
View Answer

Answer: c
Explanation: The code shown above will return the number of seconds passed since 1st January, 1970.
advertisement

4. What will be the output of the following Python code, if the time module has already been imported?

def num(m):
	t1 = time.time()
	for i in range(0,m):
		print(i)
	t2 = time.time()
	print(str(t2-t1))
 
    num(3)

a)

   1
   2
   3
   The time taken for the execution of the code
advertisement

b)

   3
   The time taken for the execution of the code

c)

   1
   2
   3
   UTC time 

d)

   3
   UTC time
View Answer
Answer: a
Explanation: The code shown above will return the numbers 1, 2, 3, followed by the time taken in the execution of the code.
Output:
1
2
3
The time taken for the execution of the code
 
 

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

import time
time.asctime()

a) Current date only
b) UTC time
c) Current date and time
d) Current time only
View Answer

Answer: c
Explanation: The function time.asctime(), present if the time module can be used to return the current date and time. It can also accept a parameter and return the date and time in a particular format. However in the above code, since we have not passed any parameters in the above code, the current date and time is returned.

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

import time
t=(2010, 9, 20, 8, 15, 12, 6)
time.asctime(t)

a) ‘20 Sep 2010 8:15:12 Sun’
b) ‘2010 20 Sept 08:15:12 Sun’
c) ‘Sun Sept 20 8:15:12 2010’
d) Error
View Answer

Answer: d
Explanation: The code shown above results in an error because this function accepts exactly 9 arguments (including day of the year and DST), but only 7 are given. Hence an error is thrown.

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

import time
t=(2010, 9, 20, 8, 45, 12, 6, 0, 0)
time.asctime(t)

a) ‘Sep 20 2010 08:45:12 Sun’
b) ‘Sun Sep 20 08:45:12 2010’
c) ’20 Sep 08:45:12 Sun 2010’
d) ‘2010 20 Sep 08:45:12 Sun’
View Answer

Answer: b
Explanation: The code shown above returns the given date and time in a particular format. Hence the output of the code shown above will be: ‘Sun Sep 20 08:45:12 2010’.

8. The sleep function (under the time module) is used to ___________
a) Pause the code for the specified number of seconds
b) Return the specified number of seconds, in terms of milliseconds
c) Stop the execution of the code
d) Return the output of the code had it been executed earlier by the specified number of seconds
View Answer

Answer: a
Explanation: The sleep function (under the time module) is used to pause the code for the specified number of seconds. The number of seconds is taken as an argument by this function.

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

import time
for i in range(0,5):
	print(i)
	time.sleep(2)

a) After an interval of 2 seconds, the numbers 1, 2, 3, 4, 5 are printed all together
b) After an interval of 2 seconds, the numbers 0, 1, 2, 3, 4 are printed all together
c) Prints the numbers 1, 2, 3, 4, 5 at an interval of 2 seconds between each number
d) Prints the numbers 0, 1, 2, 3, 4 at an interval of 2 seconds between each number
View Answer

Answer: d
Explanation: The output of the code shown above will be the numbers 0, 1, 2, 3, 4 at an interval of 2 seconds each.

10. What will be the output if we try to extract only the year from the following Python code? (time.struct_time(tm_year=2017, tm_mon=6, tm_mday=25, tm_hour=18, tm_min=26, tm_sec=6, tm_wday=6, tm_yday=176, tm_isdst=0))

import time
t=time.localtime()
print(t)

a) t[1]
b) tm_year
c) t[0]
d) t_year
View Answer

Answer: c
Explanation: To extract the year from the code shown above, we use the command t[0]. The command t[1] will return the month number (6 in the above case). The commands tm_year and t_year will result in errors.

11. State whether true or false.

s = time.time()
t= time.time()
s == t

a) True
b) False
View Answer

Answer: b
Explanation: The variables ‘s’ and ‘t’ will not be equal due to the slight difference in the time of their execution. Hence the output of this code will be: False.

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.