Python MCQ’s on Modules: SET 7

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

Check Answer

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

62. 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’

Check Answer

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

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

Check Answer

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

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

Check Answer

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

 

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

Check Answer

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

 

66. State whether true or false.

s = time.time()

t= time.time()

s == t

a) True

b) False

Check Answer

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

67. To include the use of functions which are present in the random library, we must use the option:

a) import random

b) random.h

c) import.random

d) random.random

Check Answer

Answer: a
The command import random is used to import the random module, which enables us to use the functions which are present in the random library.

 

68. The output of the following Python code is either 1 or 2.

import random

random.randint(1,2)

a) True

b) False

Check Answer

Answer: a
The function random.randint(a,b) helps us to generate an integer between ‘a’ and ‘b’, including ‘a’ and ‘b’. In this case, since there are no integers between 1 and 2, the output will necessarily be either 1 or 2’.

 

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

import random

random.choice(2,3,4)

a) An integer other than 2, 3 and 4

b) Either 2, 3 or 4

c) Error

d) 3 only

Check Answer

Answer: c
The code shown above displays the incorrect syntax of the function random.choice(). This functions takes its numeric parameter in the form of a list. Hence the correct syntax world be: random.choice([2,3,4]).

 

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

import random

random.choice([10.4, 56.99, 76])

a) Error

b) Either 10.4, 56.99 or 76

c) Any number other than 10.4, 56.99 and 76

d) 56.99 only

Check Answer

Answer: b
The function random.choice(a,b,c,d) returns a random number which is selected from a, b, c and d. The output can be either a, b, c or d. Hence the output of the snippet of code shown above can be either 10.4, 56.99 or 76.

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