250+ TOP MCQs on Datetime Module and Answers

Python Multiple Choice Questions 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

Answer: b
Clarification: 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.

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()

Answer: d
Clarification: 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?

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

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

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

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
Clarification: 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

Answer: c
Clarification: 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

Answer: d
Clarification: 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’

Answer: b
Clarification: 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

Answer: a
Clarification: 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

Answer: d
Clarification: 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

Answer: c
Clarification: 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

Answer: b
Clarification: 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.

 

Leave a Reply

Your email address will not be published. Required fields are marked *