# 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

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

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

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

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

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)

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

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

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

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