What will be the output of the following Python code if the system date is?

The datetime module supplies classes for manipulating dates and times in both simple and complex ways. datetime.now(tz=None) returns the current local date and time. If optional argument tz is None or not specified, this is like today().

What will be the output of the following Python code if the system date is?

date.strftime(format) returns a string representing the date, controlled by an explicit format string. Format codes referring to hours, minutes or seconds will see 0 values.

Sample Solution:-

Python Code:

import datetime
now = datetime.datetime.now()
print ("Current date and time : ")
print (now.strftime("%Y-%m-%d %H:%M:%S"))

Sample Output:

Current date and time : 
2014-07-05 14:34:14

Flowchart:

What will be the output of the following Python code if the system date is?

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

There are a number of ways we can take to get the current date. We will use the date class of the datetime module to accomplish this task.


Example 1: Python get today's date

from datetime import date

today = date.today()
print("Today's date:", today)

Output

Today's date: 2022-12-27

Here, we imported the date class from the datetime module. Then, we used the date.today() method to get the current local date.


Example 2: Current date in different formats

from datetime import date

today = date.today()

# dd/mm/YY
d1 = today.strftime("%d/%m/%Y")
print("d1 =", d1)

# Textual month, day and year	
d2 = today.strftime("%B %d, %Y")
print("d2 =", d2)

# mm/dd/y
d3 = today.strftime("%m/%d/%y")
print("d3 =", d3)

# Month abbreviation, day and year	
d4 = today.strftime("%b-%d-%Y")
print("d4 =", d4)

Output

d1 = 27/12/2022
d2 = December 27, 2022
d3 = 12/27/22
d4 = Dec-27-2022

If we need to get the current date and time, you can use the datetime class of the datetime module.

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

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

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

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
time.time()
0

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
time.time()
1

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.

import time
time.time()
2

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.

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

What will be the output of the following Python code if the system date is 18th August, 2016? 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.

What will be the output of the following Python code if the code is run on Windows operating system?

What will be the output of the following Python code, if the code is run on Windows operating system? Explanation: The output of the function sys. platform[:2] is equal to 'wi', when this code is run on windows operating system. Hence the output printed is 'hello'.

What will be the output of the following Python code L =[ 1 0 2 0 Hello print bool L ))?

What will be the output of the following Python code? Explanation: The code shown above returns a new list containing only those elements of the list l which do not amount to zero. Hence the output is: [1, 2, 'hello'].

What will be the output of the following Python code int (' 65.43 ')?

What will be the output of the following Python code? int('65.43')a) ImportErrorb) ValueErrorc) TypeErrord) NameErrorView AnswerAnswer: bExplanation: The snippet of code shown above results in a value error. This is because thereis an invalid literal for int() with base 10: '65.43'.