250+ TOP MCQs on Random module and Answers

Python Multiple Choice Questions on “Random module – 1”.

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

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

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

import random
random.randint(1,2)

a) True
b) False

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

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

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

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

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

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

a) sun
b) u
c) either s, u or n
d) error

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

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

a) Error
b) Either 3 or 4
c) Any integer other than 3 and 4
d) Any decimal value between 3 and 4

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

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

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

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

8. Which of the following functions helps us to randomize the items of a list?
a) seed
b) randomise
c) shuffle
d) uniform

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

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

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

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

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

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

a) 10
b) 18
c) 79
d) 95

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

12. Both the functions randint and uniform accept ____________ parameters.
a) 0
b) 1
c) 3
d) 2

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

13. The randrange function returns only an integer value.
a) True
b) False

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

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

random.randrange(1,100,10)

a) 32
b) 67
c) 91
d) 80

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

15. What will be the output of the following Python function, assuming that the random library has already been included?

a) Randomized list containing the same numbers in any order
b) The same list, that is [1,2,24]
c) A list containing any random numbers between 1 and 24
d) Error

Answer: d
Clarification: The function shown above will result in an error because this is the incorrect syntax for the usage of the function shuffle(). The list should be previously declared and then passed to this function to get an output.
An example of the correct syntax:

>>> l=['a','b','c','d']
>>> random.shuffle(l)
>>> print(l)

 

Leave a Reply

Your email address will not be published. Required fields are marked *