Python MCQs on Modules: SET 8

71. What will be the output of the following Python function (random module has already been imported)?

random.choice(‘sun’)

a) sun

b) u

c) either s, u or n

d) error

Check Answer

Answer: c
The above function works with alphabets just as it does with numbers. The output of this expression will be either s, u or n.

 

72. What will be the output of the following Python function, assuming that the random module has already been imported?

random.uniform(3,4)

a) Error

b) Either 3 or 4

c) Any integer other than 3 and 4

d) Any decimal value between 3 and 4

Check Answer

Answer: d
This question depicts the basic difference between the functions random.randint(a, b) and random.uniform(a, b). While random.randint(a,b) generates an integer between ‘a’ and ‘b’, including ‘a’ and ‘b’, the function random.uniform(a,b) generates a decimal value between ‘a’ and ‘b’.

 

73. What will be the output of the following Python function if the random module has already been imported?

random.randint(3.5,7)

a) Error

b) Any integer between 3.5 and 7, including 7

c) Any integer between 3.5 and 7, excluding 7

d) The integer closest to the mean of 3.5 and 7

Check Answer

Answer: a
The function random.randint() does not accept a decimal value as a parameter. Hence the function shown above will throw an error.

74. Which of the following functions helps us to randomize the items of a list?

a) seed

b) randomise

c) shuffle

d) uniform

Check Answer

Answer: c
The function shuffle, which is included in the random module, helps us to randomize the items of a list. This function takes the list as a parameter.

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

random.seed(3)

random.randint(1,5)

2

random.seed(3)

random.randint(1,5)

a) 3

b) 2

c) Any integer between 1 and 5, including 1 and 5

d) Any integer between 1 and 5, excluding 1 and 5

Check Answer

Answer: b
We use the seed function when we want to use the same random number once again in our program. Hence the output of the code shown above will be 2, since 2 was generated previously following which we used the seed function.

76. What is the interval of the value generated by the function random.random(), assuming that the random module has already been imported?

a) (0,1)

b) (0,1]

c) [0,1]

d) [0,1)

Check Answer

Answer: d
The function random.random() generates a random value in the interval [0,1), that is, including zero but excluding one.

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

random.randrange(0,91,5)

a) 10

b) 18

c) 79

d) 95

Check Answer

Answer: a
The function shown above will generate an output which is a multiple of 5 and is between 0 and 91. The only option which satisfies these criteria is 10. Hence the only possible output of this function is 10.

78. Both the functions randint and uniform accept ____________ parameters.

a) 0

b) 1

c) 3

d) 2

Check Answer

Answer: d
Both of these functions, that is, randint and uniform are included in the random module and both of these functions accept 2 parameters. For example: random.uniform(a,b) where ‘a’ and ‘b’ specify the range.

79. The randrange function returns only an integer value.

a) True

b) False

Check Answer

Answer: a
The function randrange returns only an integer value. Hence this statement is true.

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

random.randrange(1,100,10)

a) 32

b) 67

c) 91

d) 80

Check Answer

Answer: c
The output of this function can be any value which is a multiple of 10, plus 1. Hence a value like 11, 21, 31, 41…91 can be the output. Also, the value should necessarily be between 1 and 100. The only option which satisfies this criteria is 91.

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