This set of Python Multiple Choice Questions & Answers (MCQs) focuses 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

View Answer

Explanation: 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 snippet of code is either 1 or 2. State whether this statement is true or false.

import random random.randint(1,2)

a) True

b) False

View Answer

Explanation: 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 is the output of the code shown below?

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

View Answer

Explanation: 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 is the output of the code shown below?

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

c) 56.99 only

View Answer

Explanation: 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 is the output of the function shown below (random module has already been imported)?

random.choice('sun')

a) sun

b) u

c) either s, u or n

d) error

View Answer

Explanation: 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 is the output of the following 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

View Answer

Explanation: 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 is the output of the function shown below 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

View Answer

Explanation: 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

View Answer

Explanation: 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 is the output of the code shown below?

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

View Answer

Explanation: 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)

View Answer

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

11. Which of the following is a possible outcome of the function shown below?

random.randrange(0,91,5)

a) 10

b) 18

c) 79

d) 95

View Answer

Explanation: 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

View Answer

Explanation: Both of these functions, that is, randint and uniform are included in the random module and both of these functions accept 3 parameters. For example: random.uniform(self,a,b) where ‘a’ and ‘b’ specify the range and self is an imaginary parameter.

13. The randrange function returns only an integer value. State whether true or false.

a) True

b) False

View Answer

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

14. Which of the following options is the possible outcome of the function shown below?

random.randrange(1,100,10)

a) 32

b) 67

c) 91

d) 80

View Answer

Explanation: 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 is the output of this function, assuming that the random library has already been included?

random.shuffle[1,2,24]

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

View Answer

Explanation: 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)

**Sanfoundry Global Education & Learning Series – Python.**

To practice all areas of Python, __here is complete set of 1000+ Multiple Choice Questions and Answers__.