Python MCQ’s on Modules: SET 6

51. 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

Check Answer

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

 

52. 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

Check Answer

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

53. 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

Check Answer

Answer: c
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).

54. 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)

Check Answer

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

55. 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

Check Answer

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

56. 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

Check Answer

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

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

Check Answer

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

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

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

Check Answer

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

59. 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

Check Answer

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

 

60. 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

Check Answer

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

Page  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15      16 

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment